]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_tree.cpp
skins2: small optimisation
[vlc] / modules / gui / skins2 / controls / ctrl_tree.cpp
1 /*****************************************************************************
2  * ctrl_tree.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <math.h>
26 #include "../utils/var_bool.hpp"
27 #include "ctrl_tree.hpp"
28 #include "../src/os_factory.hpp"
29 #include "../src/os_graphics.hpp"
30 #include "../src/generic_bitmap.hpp"
31 #include "../src/generic_font.hpp"
32 #include "../src/scaled_bitmap.hpp"
33 #include "../utils/position.hpp"
34 #include "../utils/ustring.hpp"
35 #include "../events/evt_key.hpp"
36 #include "../events/evt_mouse.hpp"
37 #include "../events/evt_scroll.hpp"
38 #include <vlc_keys.h>
39
40 #define SCROLL_STEP 0.05
41 #define LINE_INTERVAL 1  // Number of pixels inserted between 2 lines
42
43
44 CtrlTree::CtrlTree( intf_thread_t *pIntf,
45                     VarTree &rTree,
46                     const GenericFont &rFont,
47                     const GenericBitmap *pBgBitmap,
48                     const GenericBitmap *pItemBitmap,
49                     const GenericBitmap *pOpenBitmap,
50                     const GenericBitmap *pClosedBitmap,
51                     uint32_t fgColor,
52                     uint32_t playColor,
53                     uint32_t bgColor1,
54                     uint32_t bgColor2,
55                     uint32_t selColor,
56                     const UString &rHelp,
57                     VarBool *pVisible,
58                     VarBool *pFlat ):
59     CtrlGeneric( pIntf,rHelp, pVisible), m_rTree( rTree), m_rFont( rFont ),
60     m_pBgBitmap( pBgBitmap ), m_pItemBitmap( pItemBitmap ),
61     m_pOpenBitmap( pOpenBitmap ), m_pClosedBitmap( pClosedBitmap ),
62     m_fgColor( fgColor ), m_playColor( playColor ), m_bgColor1( bgColor1 ),
63     m_bgColor2( bgColor2 ), m_selColor( selColor ),
64     m_pLastSelected( NULL ), m_pImage( NULL ), m_dontMove( false )
65 {
66     // Observe the tree and position variables
67     m_rTree.addObserver( this );
68     m_rTree.getPositionVar().addObserver( this );
69
70     m_flat = pFlat->get();
71
72     m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
73
74     makeImage();
75 }
76
77 CtrlTree::~CtrlTree()
78 {
79     m_rTree.getPositionVar().delObserver( this );
80     m_rTree.delObserver( this );
81     delete m_pImage;
82 }
83
84 int CtrlTree::itemHeight()
85 {
86     int itemHeight = m_rFont.getSize();
87     if( !m_flat )
88     {
89         if( m_pClosedBitmap )
90         {
91             itemHeight = __MAX( m_pClosedBitmap->getHeight(), itemHeight );
92         }
93         if( m_pOpenBitmap )
94         {
95             itemHeight = __MAX( m_pOpenBitmap->getHeight(), itemHeight );
96         }
97     }
98     if( m_pItemBitmap )
99     {
100         itemHeight = __MAX( m_pItemBitmap->getHeight(), itemHeight );
101     }
102     itemHeight += LINE_INTERVAL;
103     return itemHeight;
104 }
105
106 int CtrlTree::itemImageWidth()
107 {
108     int bitmapWidth = 5;
109     if( !m_flat )
110     {
111         if( m_pClosedBitmap )
112         {
113             bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
114         }
115         if( m_pOpenBitmap )
116         {
117             bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
118         }
119     }
120     if( m_pItemBitmap )
121     {
122         bitmapWidth = __MAX( m_pItemBitmap->getWidth(), bitmapWidth );
123     }
124     return bitmapWidth + 2;
125 }
126
127 int CtrlTree::maxItems()
128 {
129     const Position *pPos = getPosition();
130     if( !pPos )
131     {
132         return -1;
133     }
134     return pPos->getHeight() / itemHeight();
135 }
136
137
138 void CtrlTree::onUpdate( Subject<VarTree, tree_update> &rTree,
139                          tree_update *arg )
140 {
141     if( arg->i_type == 0 ) // Item update
142     {
143         if( arg->b_active_item )
144         {
145             autoScroll();
146             ///\todo We should make image if we are visible in the view
147             makeImage();
148         }
149     }
150     /// \todo handle delete in a more clever way
151     else if ( arg->i_type == 1 ) // Global change or deletion
152     {
153         m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
154
155         makeImage();
156     }
157     else if ( arg->i_type == 2 ) // Item-append
158     {
159         if( m_flat && m_firstPos->size() )
160             m_firstPos = m_rTree.getNextLeaf( m_firstPos );
161         /// \todo Check if the item is really visible in the view
162         // (we only check if it in the document)
163         if( arg->b_visible == true )
164         {
165             makeImage();
166         }
167     }
168     else if( arg->i_type == 3 ) // item-del
169     {
170         /* Make sure firstPos is valid */
171         while( m_firstPos->m_deleted &&
172                m_firstPos != (m_flat ? m_rTree.firstLeaf()
173                                      : m_rTree.begin()) )
174         {
175             m_firstPos = m_flat ? m_rTree.getPrevLeaf( m_firstPos )
176                                 : m_rTree.getPrevVisibleItem( m_firstPos );
177         }
178         if( m_firstPos->m_deleted )
179             m_firstPos = m_rTree.begin();
180
181         if( arg->b_visible == true )
182         {
183             makeImage();
184         }
185     }
186     notifyLayout();
187 }
188
189 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent, void* arg)
190 {
191     // Determine what is the first item to display
192     VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
193
194     if( m_dontMove ) return;
195
196     int excessItems;
197     if( m_flat )
198         excessItems = m_rTree.countLeafs() - maxItems();
199     else
200         excessItems = m_rTree.visibleItems() - maxItems();
201
202     if( excessItems > 0)
203     {
204         VarPercent &rVarPos = m_rTree.getPositionVar();
205         // a simple (int)(...) causes rounding errors !
206 #ifdef _MSC_VER
207 #   define lrint (int)
208 #endif
209         if( m_flat )
210             it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
211         else
212             it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
213     }
214     if( m_firstPos != it )
215     {
216         // Redraw the control if the position has changed
217         m_firstPos = it;
218         makeImage();
219         notifyLayout();
220     }
221 }
222
223 void CtrlTree::onResize()
224 {
225     // Determine what is the first item to display
226     VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
227
228     int excessItems;
229     if( m_flat )
230         excessItems = m_rTree.countLeafs() - maxItems();
231     else
232         excessItems = m_rTree.visibleItems() - maxItems();
233
234     if( excessItems > 0)
235     {
236         VarPercent &rVarPos = m_rTree.getPositionVar();
237         // a simple (int)(...) causes rounding errors !
238 #ifdef _MSC_VER
239 #   define lrint (int)
240 #endif
241         if( m_flat )
242             it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
243         else
244             it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
245     }
246     // Redraw the control if the position has changed
247     m_firstPos = it;
248     makeImage();
249 }
250
251 void CtrlTree::onPositionChange()
252 {
253     makeImage();
254 }
255
256 void CtrlTree::handleEvent( EvtGeneric &rEvent )
257 {
258     bool bChangedPosition = false;
259     VarTree::Iterator toShow; bool needShow = false;
260     if( rEvent.getAsString().find( "key:down" ) != string::npos )
261     {
262         int key = ((EvtKey&)rEvent).getKey();
263         VarTree::Iterator it;
264         bool previousWasSelected = false;
265
266         /* Delete the selection */
267         if( key == KEY_DELETE )
268         {
269             /* Find first non selected item before m_pLastSelected */
270             VarTree::Iterator it_sel = m_flat ? m_rTree.firstLeaf()
271                                               : m_rTree.begin();
272             for( it = (m_flat ? m_rTree.firstLeaf() : m_rTree.begin());
273                  it != m_rTree.end();
274                  it = (m_flat ? m_rTree.getNextLeaf( it )
275                               : m_rTree.getNextVisibleItem( it )) )
276             {
277                 if( &*it == m_pLastSelected ) break;
278                 if( !it->m_selected ) it_sel = it;
279             }
280
281             /* Delete selected stuff */
282             m_rTree.delSelected();
283
284             /* Verify if there is still sthg selected (e.g read-only items) */
285             m_pLastSelected = NULL;
286             for( it = (m_flat ? m_rTree.firstLeaf() : m_rTree.begin());
287                  it != m_rTree.end();
288                  it = (m_flat ? m_rTree.getNextLeaf( it )
289                               : m_rTree.getNextVisibleItem( it )) )
290             {
291                 if( it->m_selected )
292                     m_pLastSelected = &*it;
293             }
294
295             /* if everything was deleted, use it_sel as last selection */
296             if( !m_pLastSelected )
297             {
298                 it_sel->m_selected = true;
299                 m_pLastSelected = &*it_sel;
300             }
301
302             // Redraw the control
303             makeImage();
304             notifyLayout();
305         }
306         else if( key == KEY_PAGEDOWN )
307         {
308             it = m_firstPos;
309             int i = (int)(maxItems()*1.5);
310             while( i >= 0 )
311             {
312                 VarTree::Iterator it_old = it;
313                 it = m_flat ? m_rTree.getNextLeaf( it )
314                             : m_rTree.getNextVisibleItem( it );
315                 /* End is already visible, dont' scroll */
316                 if( it == m_rTree.end() )
317                 {
318                     it = it_old;
319                     break;
320                 }
321                 needShow = true;
322                 i--;
323             }
324             if( needShow )
325             {
326                 ensureVisible( it );
327                 makeImage();
328                 notifyLayout();
329             }
330         }
331         else if (key == KEY_PAGEUP )
332         {
333             it = m_firstPos;
334             int i = maxItems();
335             while( i >= maxItems()/2 )
336             {
337                 it = m_flat ? m_rTree.getPrevLeaf( it )
338                             : m_rTree.getPrevVisibleItem( it );
339                 /* End is already visible, dont' scroll */
340                 if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
341                 {
342                     break;
343                 }
344                 i--;
345             }
346             ensureVisible( it );
347             makeImage();
348             notifyLayout();
349         }
350         else if ( key == KEY_UP ||
351                   key == KEY_DOWN ||
352                   key == KEY_LEFT ||
353                   key == KEY_RIGHT ||
354                   key == KEY_ENTER ||
355                   key == ' ' )
356         {
357             for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
358                  it != m_rTree.end();
359                  it = m_flat ? m_rTree.getNextLeaf( it )
360                              : m_rTree.getNextVisibleItem( it ) )
361             {
362                 VarTree::Iterator next = m_flat ?
363                                          m_rTree.getNextLeaf( it ) :
364                                          m_rTree.getNextVisibleItem( it );
365                 if( key == KEY_UP )
366                 {
367                     // Scroll up one item
368                     if( ( it->parent()
369                           && it != it->parent()->begin() )
370                         || &*it != m_pLastSelected )
371                     {
372                         bool nextWasSelected = ( &*next == m_pLastSelected );
373                         it->m_selected = nextWasSelected;
374                         if( nextWasSelected )
375                         {
376                             m_pLastSelected = &*it;
377                             needShow = true; toShow = it;
378                         }
379                     }
380                 }
381                 else if( key == KEY_DOWN )
382                 {
383                     // Scroll down one item
384                     if( ( it->parent()
385                           && next != it->parent()->end() )
386                         || &*it != m_pLastSelected )
387                     {
388                         (*it).m_selected = previousWasSelected;
389                     }
390                     if( previousWasSelected )
391                     {
392                         m_pLastSelected = &*it;
393                         needShow = true; toShow = it;
394                         previousWasSelected = false;
395                     }
396                     else
397                     {
398                         previousWasSelected = ( &*it == m_pLastSelected );
399                     }
400
401                     // Fix last tree item selection
402                     if( ( m_flat ? m_rTree.getNextLeaf( it )
403                         : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
404                      && &*it == m_pLastSelected )
405                     {
406                         (*it).m_selected = true;
407                     }
408                 }
409                 else if( key == KEY_RIGHT )
410                 {
411                     // Go down one level (and expand node)
412                     if( &*it == m_pLastSelected )
413                     {
414                         if( it->m_expanded )
415                         {
416                             if( it->size() )
417                             {
418                                 it->m_selected = false;
419                                 it->begin()->m_selected = true;
420                                 m_pLastSelected = &*(it->begin());
421                             }
422                             else
423                             {
424                                 m_rTree.action( &*it );
425                             }
426                         }
427                         else
428                         {
429                             it->m_expanded = true;
430                             bChangedPosition = true;
431                         }
432                     }
433                 }
434                 else if( key == KEY_LEFT )
435                 {
436                     // Go up one level (and close node)
437                     if( &*it == m_pLastSelected )
438                     {
439                         if( it->m_expanded && it->size() )
440                         {
441                             it->m_expanded = false;
442                             bChangedPosition = true;
443                         }
444                         else
445                         {
446                             if( it->parent() && it->parent() != &m_rTree)
447                             {
448                                 it->m_selected = false;
449                                 m_pLastSelected = it->parent();
450                                 m_pLastSelected->m_selected = true;
451                             }
452                         }
453                     }
454                 }
455                 else if( key == KEY_ENTER || key == ' ' )
456                 {
457                     // Go up one level (and close node)
458                     if( &*it == m_pLastSelected )
459                     {
460                         m_rTree.action( &*it );
461                     }
462                 }
463             }
464             if( needShow )
465                 ensureVisible( toShow );
466             // Redraw the control
467             makeImage();
468             notifyLayout();
469         }
470         else
471         {
472             // other keys to be forwarded to vlc core
473             EvtKey& rEvtKey = (EvtKey&)rEvent;
474             var_SetInteger( getIntf()->p_libvlc, "key-pressed",
475                             rEvtKey.getModKey() );
476         }
477
478     }
479
480     else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
481     {
482         EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
483         const Position *pos = getPosition();
484         int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
485         int xPos = rEvtMouse.getXPos() - pos->getLeft();
486         VarTree::Iterator it;
487
488         if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
489             string::npos )
490         {
491             VarTree::Iterator itClicked = findItemAtPos( yPos );
492             // Flag to know if the current item must be selected
493             bool select = false;
494             for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
495                  it != m_rTree.end();
496                  it = m_flat ? m_rTree.getNextLeaf( it )
497                              : m_rTree.getNextVisibleItem( it ) )
498             {
499                 bool nextSelect = select;
500                 if( it == itClicked || &*it == m_pLastSelected )
501                 {
502                     if( select )
503                     {
504                         nextSelect = false;
505                     }
506                     else
507                     {
508                         select = true;
509                         nextSelect = true;
510                     }
511                 }
512                 it->m_selected = (*it).m_selected || select;
513                 select = nextSelect;
514             }
515             // Redraw the control
516             makeImage();
517             notifyLayout();
518         }
519         else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
520                  string::npos )
521         {
522             // Invert the selection of the item
523             it = findItemAtPos( yPos );
524             if( it != m_rTree.end() )
525             {
526                 it->m_selected = !it->m_selected;
527                 m_pLastSelected = &*it;
528             }
529             // Redraw the control
530             makeImage();
531             notifyLayout();
532         }
533         else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
534                  string::npos )
535         {
536             VarTree::Iterator itClicked = findItemAtPos( yPos );
537             // Flag to know if the current item must be selected
538             bool select = false;
539             for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
540                  it != m_rTree.end();
541                  it = m_flat ? m_rTree.getNextLeaf( it )
542                              : m_rTree.getNextVisibleItem( it ) )
543             {
544                 bool nextSelect = select;
545                 if( it == itClicked || &*it == m_pLastSelected )
546                 {
547                     if( select )
548                     {
549                         nextSelect = false;
550                     }
551                     else
552                     {
553                         select = true;
554                         nextSelect = true;
555                     }
556                 }
557                 it->m_selected = select;
558                 select = nextSelect;
559             }
560             // Redraw the control
561             makeImage();
562             notifyLayout();
563         }
564         else if( rEvent.getAsString().find( "mouse:left:down" ) !=
565                  string::npos )
566         {
567             it = findItemAtPos(yPos);
568             if( it != m_rTree.end() )
569             {
570                 if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
571                       && xPos < it->depth() * itemImageWidth() )
572                  && !m_flat )
573                 {
574                     // Fold/unfold the item
575                     it->m_expanded = !it->m_expanded;
576                     bChangedPosition = true;
577                 }
578                 else
579                 {
580                     // Unselect any previously selected item
581                     VarTree::Iterator it2;
582                     for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
583                          it2 != m_rTree.end();
584                          it2 = m_flat ? m_rTree.getNextLeaf( it2 )
585                                       : m_rTree.getNextVisibleItem( it2 ) )
586                     {
587                         it2->m_selected = false;
588                     }
589                     // Select the new item
590                     if( it != m_rTree.end() )
591                     {
592                         it->m_selected = true;
593                         m_pLastSelected = &*it;
594                     }
595                 }
596             }
597             // Redraw the control
598             makeImage();
599             notifyLayout();
600         }
601         else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
602                  string::npos )
603         {
604             it = findItemAtPos(yPos);
605             if( it != m_rTree.end() )
606             {
607                // Execute the action associated to this item
608                m_rTree.action( &*it );
609             }
610             // Redraw the control
611             makeImage();
612             notifyLayout();
613         }
614     }
615
616     else if( rEvent.getAsString().find( "scroll" ) != string::npos )
617     {
618         // XXX ctrl_slider.cpp has two more (but slightly different)
619         // XXX implementations of `scroll'. Figure out where it belongs.
620
621         int direction = static_cast<EvtScroll&>(rEvent).getDirection();
622
623         double percentage = m_rTree.getPositionVar().get();
624         double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
625                                              : m_rTree.visibleItems() );
626         if( direction == EvtScroll::kUp )
627         {
628             percentage += step;
629         }
630         else
631         {
632             percentage -= step;
633         }
634         m_rTree.getPositionVar().set( percentage );
635     }
636
637     /* We changed the nodes, let's fix the position var */
638     if( bChangedPosition )
639     {
640         VarTree::Iterator it;
641         int iFirst = 0;
642         for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
643              it != m_rTree.end();
644              it = m_flat ? m_rTree.getNextLeaf( it )
645                          : m_rTree.getNextVisibleItem( it ) )
646         {
647             if( it == m_firstPos )
648                 break;
649             iFirst++;
650         }
651
652         int indexMax = ( m_flat ? m_rTree.countLeafs()
653                                 : m_rTree.visibleItems() ) - 1;
654         float f_new = (float)iFirst / (float)indexMax;
655
656         m_dontMove = true;
657         m_rTree.getPositionVar().set( 1.0 - f_new );
658         m_dontMove = false;
659     }
660 }
661
662 bool CtrlTree::mouseOver( int x, int y ) const
663 {
664     const Position *pPos = getPosition();
665     return !pPos ? false :
666         x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight();
667 }
668
669 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h)
670 {
671     const Position *pPos = getPosition();
672     rect region( pPos->getLeft(), pPos->getTop(),
673                  pPos->getWidth(), pPos->getHeight() );
674     rect clip( xDest, yDest, w, h );
675     rect inter;
676
677     if( rect::intersect( region, clip, &inter ) && m_pImage )
678         rImage.drawGraphics( *m_pImage,
679                       inter.x - pPos->getLeft(),
680                       inter.y - pPos->getTop(),
681                       inter.x, inter.y, inter.width, inter.height );
682 }
683
684 bool CtrlTree::ensureVisible( VarTree::Iterator item )
685 {
686     // Find the item to focus
687     int focusItemIndex = 0;
688     VarTree::Iterator it;
689
690     m_rTree.ensureExpanded( item );
691
692     for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
693          it != m_rTree.end();
694          it = m_flat ? m_rTree.getNextLeaf( it )
695                      : m_rTree.getNextVisibleItem( it ) )
696     {
697         if( it->m_id == item->m_id ) break;
698         focusItemIndex++;
699     }
700    return ensureVisible( focusItemIndex );
701 }
702
703 bool CtrlTree::ensureVisible( int focusItemIndex )
704 {
705     // Find  m_firstPos
706     VarTree::Iterator it;
707     int firstPosIndex = 0;
708     for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
709          it != m_rTree.end();
710          it = m_flat ? m_rTree.getNextLeaf( it )
711                      : m_rTree.getNextVisibleItem( it ) )
712     {
713         if( it == m_firstPos ) break;
714         firstPosIndex++;
715     }
716
717     if( it == m_rTree.end() ) return false;
718
719
720     if( it != m_rTree.end()
721         && ( focusItemIndex < firstPosIndex
722            || focusItemIndex > firstPosIndex + maxItems() - 1 ) )
723     {
724         // Scroll to have the wanted stream visible
725         VarPercent &rVarPos = m_rTree.getPositionVar();
726         int indexMax = ( m_flat ? m_rTree.countLeafs()
727                                 : m_rTree.visibleItems() ) - 1;
728         rVarPos.set( 1.0 - (double)focusItemIndex / (double)indexMax );
729         return true;
730     }
731     return false;
732 }
733
734 void CtrlTree::autoScroll()
735 {
736     // Find the current playing stream
737     int playIndex = 0;
738     VarTree::Iterator it;
739
740     for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
741          it != m_rTree.end();
742          it = m_flat ? m_rTree.getNextLeaf( it )
743                      : m_rTree.getNextItem( it ) )
744     {
745         if( it->m_playing )
746         {
747            m_rTree.ensureExpanded( it );
748            break;
749         }
750     }
751
752     for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
753          it != m_rTree.end();
754          it = m_flat ? m_rTree.getNextLeaf( it )
755                      : m_rTree.getNextVisibleItem( it ) )
756     {
757         if( it->m_playing )
758         {
759            ensureVisible( playIndex );
760            break;
761         }
762         playIndex++;
763     }
764 }
765
766
767 void CtrlTree::makeImage()
768 {
769     stats_TimerStart( getIntf(), "[Skins] Playlist image",
770                       STATS_TIMER_SKINS_PLAYTREE_IMAGE );
771     delete m_pImage;
772
773     // Get the size of the control
774     const Position *pPos = getPosition();
775     if( !pPos )
776     {
777         stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
778         return;
779     }
780     int width = pPos->getWidth();
781     int height = pPos->getHeight();
782
783     int i_itemHeight = itemHeight();
784
785     // Create an image
786     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
787     m_pImage = pOsFactory->createOSGraphics( width, height );
788
789     VarTree::Iterator it = m_firstPos;
790
791     if( m_pBgBitmap )
792     {
793         // Draw the background bitmap
794         ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
795         m_pImage->drawBitmap( bmp, 0, 0 );
796
797         for( int yPos = 0; yPos < height; yPos += i_itemHeight )
798         {
799             if( it != m_rTree.end() )
800             {
801                 if( (*it).m_selected )
802                 {
803                     int rectHeight = __MIN( i_itemHeight, height - yPos );
804                     m_pImage->fillRect( 0, yPos, width, rectHeight,
805                                         m_selColor );
806                 }
807                 do
808                 {
809                     it = m_flat ? m_rTree.getNextLeaf( it )
810                                 : m_rTree.getNextVisibleItem( it );
811                 } while( it != m_rTree.end() && it->m_deleted );
812             }
813         }
814     }
815     else
816     {
817         // FIXME (TRYME)
818         // Fill background with background color
819         uint32_t bgColor = m_bgColor1;
820         m_pImage->fillRect( 0, 0, width, height, bgColor );
821         for( int yPos = 0; yPos < height; yPos += i_itemHeight )
822         {
823             int rectHeight = __MIN( i_itemHeight, height - yPos );
824             if( it == m_rTree.end() )
825                 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
826             else
827             {
828                 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
829                 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
830                 do
831                 {
832                     it = m_flat ? m_rTree.getNextLeaf( it )
833                                 : m_rTree.getNextVisibleItem( it );
834                 } while( it != m_rTree.end() && it->m_deleted );
835             }
836             bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
837         }
838     }
839
840     int bitmapWidth = itemImageWidth();
841
842     int yPos = 0;
843     it = m_firstPos;
844     while( it != m_rTree.end() && yPos < height )
845     {
846         const GenericBitmap *m_pCurBitmap;
847         UString *pStr = (UString*)(it->m_cString.get());
848         uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
849
850         // Draw the text
851         if( pStr != NULL )
852         {
853             int depth = m_flat ? 1 : it->depth();
854             GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
855             if( !pText )
856             {
857                 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
858                 return;
859             }
860             if( it->size() )
861                 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
862             else
863                 m_pCurBitmap = m_pItemBitmap;
864
865             if( m_pCurBitmap )
866             {
867                 // Make sure we are centered on the line
868                 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
869                 if( yPos2 >= height )
870                 {
871                     delete pText;
872                     break;
873                 }
874                 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
875                                       bitmapWidth * (depth - 1 ), yPos2,
876                                       m_pCurBitmap->getWidth(),
877                                       __MIN( m_pCurBitmap->getHeight(),
878                                              height -  yPos2), true );
879             }
880             yPos += i_itemHeight - pText->getHeight();
881             int ySrc = 0;
882             if( yPos < 0 )
883             {
884                 ySrc = - yPos;
885                 yPos = 0;
886             }
887             int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
888             m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
889                                   pText->getWidth(),
890                                   lineHeight, true );
891             yPos += (pText->getHeight() - ySrc );
892             delete pText;
893         }
894         do
895         {
896             it = m_flat ? m_rTree.getNextLeaf( it )
897                 : m_rTree.getNextVisibleItem( it );
898         } while( it != m_rTree.end() && it->m_deleted );
899     }
900     stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
901 }
902
903 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
904 {
905     // The first item is m_firstPos.
906     // We decrement pos as we try the other items, until pos == 0.
907     VarTree::Iterator it;
908     for( it = m_firstPos; it != m_rTree.end() && pos != 0;
909          it = m_flat ? m_rTree.getNextLeaf( it )
910                      : m_rTree.getNextVisibleItem( it ) )
911     {
912         pos--;
913     }
914
915     return it;
916 }