]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_tree.cpp
skins2: playlist (cosmetic)
[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     m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
142
143     if( arg->i_type == 0 ) // Item update
144     {
145         if( arg->b_active_item )
146         {
147             autoScroll();
148             ///\todo We should make image if we are visible in the view
149             makeImage();
150         }
151     }
152     /// \todo handle delete in a more clever way
153     else if ( arg->i_type == 1 ) // Global change or deletion
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 and lastSelected are still valid */
171         while( m_firstPos->m_deleted && m_firstPos != m_rTree.root()->begin() )
172         {
173             m_firstPos = m_flat ? m_rTree.getPrevLeaf( m_firstPos )
174                                 : m_rTree.getPrevVisibleItem( m_firstPos );
175         }
176         if( m_firstPos->m_deleted )
177             m_firstPos = m_flat ? m_rTree.firstLeaf()
178                                 : m_rTree.root()->begin();
179
180         if( arg->b_visible == true )
181         {
182             makeImage();
183         }
184     }
185     notifyLayout();
186 }
187
188 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent, void* arg)
189 {
190     // Determine what is the first item to display
191     VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
192
193     if( m_dontMove ) return;
194
195     int excessItems;
196     if( m_flat )
197         excessItems = m_rTree.countLeafs() - maxItems();
198     else
199         excessItems = m_rTree.visibleItems() - maxItems();
200
201     if( excessItems > 0)
202     {
203         VarPercent &rVarPos = m_rTree.getPositionVar();
204         // a simple (int)(...) causes rounding errors !
205 #ifdef _MSC_VER
206 #   define lrint (int)
207 #endif
208         if( m_flat )
209             it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
210         else
211             it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
212     }
213     if( m_firstPos != it )
214     {
215         // Redraw the control if the position has changed
216         m_firstPos = it;
217         makeImage();
218         notifyLayout();
219     }
220 }
221
222 void CtrlTree::onResize()
223 {
224     // Determine what is the first item to display
225     VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
226
227     int excessItems;
228     if( m_flat )
229         excessItems = m_rTree.countLeafs() - maxItems();
230     else
231         excessItems = m_rTree.visibleItems() - maxItems();
232
233     if( excessItems > 0)
234     {
235         VarPercent &rVarPos = m_rTree.getPositionVar();
236         // a simple (int)(...) causes rounding errors !
237 #ifdef _MSC_VER
238 #   define lrint (int)
239 #endif
240         if( m_flat )
241             it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
242         else
243             it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
244     }
245     // Redraw the control if the position has changed
246     m_firstPos = it;
247     makeImage();
248 }
249
250 void CtrlTree::onPositionChange()
251 {
252     makeImage();
253 }
254
255 void CtrlTree::handleEvent( EvtGeneric &rEvent )
256 {
257     bool bChangedPosition = false;
258     VarTree::Iterator toShow; bool needShow = false;
259     if( rEvent.getAsString().find( "key:down" ) != string::npos )
260     {
261         int key = ((EvtKey&)rEvent).getKey();
262         VarTree::Iterator it;
263         bool previousWasSelected = false;
264
265         /* Delete the selection */
266         if( key == KEY_DELETE )
267         {
268             /* Find first non selected item before m_pLastSelected */
269             VarTree::Iterator it_sel = m_flat ? m_rTree.firstLeaf()
270                                               : m_rTree.begin();
271             for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
272                  it != m_rTree.end();
273                  it = m_flat ? m_rTree.getNextLeaf( it )
274                              : m_rTree.getNextVisibleItem( it ) )
275             {
276                 if( &*it == m_pLastSelected ) break;
277                 if( !it->m_selected ) it_sel = it;
278             }
279
280             /* Delete selected stuff */
281             m_rTree.delSelected();
282
283             /* Select it_sel */
284             it_sel->m_selected = true;
285             m_pLastSelected = &*it_sel;
286
287             // Redraw the control
288             makeImage();
289             notifyLayout();
290         }
291         else if( key == KEY_PAGEDOWN )
292         {
293             it = m_firstPos;
294             int i = (int)(maxItems()*1.5);
295             while( i >= 0 )
296             {
297                 VarTree::Iterator it_old = it;
298                 it = m_flat ? m_rTree.getNextLeaf( it )
299                             : m_rTree.getNextVisibleItem( it );
300                 /* End is already visible, dont' scroll */
301                 if( it == m_rTree.end() )
302                 {
303                     it = it_old;
304                     break;
305                 }
306                 needShow = true;
307                 i--;
308             }
309             if( needShow )
310             {
311                 ensureVisible( it );
312                 makeImage();
313                 notifyLayout();
314             }
315         }
316         else if (key == KEY_PAGEUP )
317         {
318             it = m_firstPos;
319             int i = maxItems();
320             while( i >= maxItems()/2 )
321             {
322                 it = m_flat ? m_rTree.getPrevLeaf( it )
323                             : m_rTree.getPrevVisibleItem( it );
324                 /* End is already visible, dont' scroll */
325                 if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
326                 {
327                     break;
328                 }
329                 i--;
330             }
331             ensureVisible( it );
332             makeImage();
333             notifyLayout();
334         }
335         else if ( key == KEY_UP ||
336                   key == KEY_DOWN ||
337                   key == KEY_LEFT ||
338                   key == KEY_RIGHT ||
339                   key == KEY_ENTER ||
340                   key == ' ' )
341         {
342             for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
343                  it != m_rTree.end();
344                  it = m_flat ? m_rTree.getNextLeaf( it )
345                              : m_rTree.getNextVisibleItem( it ) )
346             {
347                 VarTree::Iterator next = m_flat ?
348                                          m_rTree.getNextLeaf( it ) :
349                                          m_rTree.getNextVisibleItem( it );
350                 if( key == KEY_UP )
351                 {
352                     // Scroll up one item
353                     if( ( it->parent()
354                           && it != it->parent()->begin() )
355                         || &*it != m_pLastSelected )
356                     {
357                         bool nextWasSelected = ( &*next == m_pLastSelected );
358                         it->m_selected = nextWasSelected;
359                         if( nextWasSelected )
360                         {
361                             m_pLastSelected = &*it;
362                             needShow = true; toShow = it;
363                         }
364                     }
365                 }
366                 else if( key == KEY_DOWN )
367                 {
368                     // Scroll down one item
369                     if( ( it->parent()
370                           && next != it->parent()->end() )
371                         || &*it != m_pLastSelected )
372                     {
373                         (*it).m_selected = previousWasSelected;
374                     }
375                     if( previousWasSelected )
376                     {
377                         m_pLastSelected = &*it;
378                         needShow = true; toShow = it;
379                         previousWasSelected = false;
380                     }
381                     else
382                     {
383                         previousWasSelected = ( &*it == m_pLastSelected );
384                     }
385
386                     // Fix last tree item selection
387                     if( ( m_flat ? m_rTree.getNextLeaf( it )
388                         : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
389                      && &*it == m_pLastSelected )
390                     {
391                         (*it).m_selected = true;
392                     }
393                 }
394                 else if( key == KEY_RIGHT )
395                 {
396                     // Go down one level (and expand node)
397                     if( &*it == m_pLastSelected )
398                     {
399                         if( it->m_expanded )
400                         {
401                             if( it->size() )
402                             {
403                                 it->m_selected = false;
404                                 it->begin()->m_selected = true;
405                                 m_pLastSelected = &*(it->begin());
406                             }
407                             else
408                             {
409                                 m_rTree.action( &*it );
410                             }
411                         }
412                         else
413                         {
414                             it->m_expanded = true;
415                             bChangedPosition = true;
416                         }
417                     }
418                 }
419                 else if( key == KEY_LEFT )
420                 {
421                     // Go up one level (and close node)
422                     if( &*it == m_pLastSelected )
423                     {
424                         if( it->m_expanded && it->size() )
425                         {
426                             it->m_expanded = false;
427                             bChangedPosition = true;
428                         }
429                         else
430                         {
431                             if( it->parent() && it->parent() != &m_rTree)
432                             {
433                                 it->m_selected = false;
434                                 m_pLastSelected = it->parent();
435                                 m_pLastSelected->m_selected = true;
436                             }
437                         }
438                     }
439                 }
440                 else if( key == KEY_ENTER || key == ' ' )
441                 {
442                     // Go up one level (and close node)
443                     if( &*it == m_pLastSelected )
444                     {
445                         m_rTree.action( &*it );
446                     }
447                 }
448             }
449             if( needShow )
450                 ensureVisible( toShow );
451             // Redraw the control
452             makeImage();
453             notifyLayout();
454         }
455         else
456         {
457             // other keys to be forwarded to vlc core
458             EvtKey& rEvtKey = (EvtKey&)rEvent;
459             var_SetInteger( getIntf()->p_libvlc, "key-pressed",
460                             rEvtKey.getModKey() );
461         }
462
463     }
464
465     else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
466     {
467         EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
468         const Position *pos = getPosition();
469         int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
470         int xPos = rEvtMouse.getXPos() - pos->getLeft();
471         VarTree::Iterator it;
472
473         if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
474             string::npos )
475         {
476             VarTree::Iterator itClicked = findItemAtPos( yPos );
477             // Flag to know if the current item must be selected
478             bool select = false;
479             for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
480                  it != m_rTree.end();
481                  it = m_flat ? m_rTree.getNextLeaf( it )
482                              : m_rTree.getNextVisibleItem( it ) )
483             {
484                 bool nextSelect = select;
485                 if( it == itClicked || &*it == m_pLastSelected )
486                 {
487                     if( select )
488                     {
489                         nextSelect = false;
490                     }
491                     else
492                     {
493                         select = true;
494                         nextSelect = true;
495                     }
496                 }
497                 it->m_selected = (*it).m_selected || select;
498                 select = nextSelect;
499             }
500         }
501         else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
502                  string::npos )
503         {
504             // Invert the selection of the item
505             it = findItemAtPos( yPos );
506             if( it != m_rTree.end() )
507             {
508                 it->m_selected = !it->m_selected;
509                 m_pLastSelected = &*it;
510             }
511         }
512         else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
513                  string::npos )
514         {
515             VarTree::Iterator itClicked = findItemAtPos( yPos );
516             // Flag to know if the current item must be selected
517             bool select = false;
518             for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
519                  it != m_rTree.end();
520                  it = m_flat ? m_rTree.getNextLeaf( it )
521                              : m_rTree.getNextVisibleItem( it ) )
522             {
523                 bool nextSelect = select;
524                 if( it == itClicked || &*it == m_pLastSelected )
525                 {
526                     if( select )
527                     {
528                         nextSelect = false;
529                     }
530                     else
531                     {
532                         select = true;
533                         nextSelect = true;
534                     }
535                 }
536                 it->m_selected = select;
537                 select = nextSelect;
538             }
539         }
540         else if( rEvent.getAsString().find( "mouse:left:down" ) !=
541                  string::npos )
542         {
543             it = findItemAtPos(yPos);
544             if( it != m_rTree.end() )
545             {
546                 if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
547                       && xPos < it->depth() * itemImageWidth() )
548                  && !m_flat )
549                 {
550                     // Fold/unfold the item
551                     it->m_expanded = !it->m_expanded;
552                     bChangedPosition = true;
553                 }
554                 else
555                 {
556                     // Unselect any previously selected item
557                     VarTree::Iterator it2;
558                     for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
559                          it2 != m_rTree.end();
560                          it2 = m_flat ? m_rTree.getNextLeaf( it2 )
561                                       : m_rTree.getNextVisibleItem( it2 ) )
562                     {
563                         it2->m_selected = false;
564                     }
565                     // Select the new item
566                     if( it != m_rTree.end() )
567                     {
568                         it->m_selected = true;
569                         m_pLastSelected = &*it;
570                     }
571                 }
572             }
573         }
574
575         else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
576                  string::npos )
577         {
578             it = findItemAtPos(yPos);
579             if( it != m_rTree.end() )
580             {
581                // Execute the action associated to this item
582                m_rTree.action( &*it );
583             }
584         }
585         // Redraw the control
586         makeImage();
587         notifyLayout();
588     }
589
590     else if( rEvent.getAsString().find( "scroll" ) != string::npos )
591     {
592         // XXX ctrl_slider.cpp has two more (but slightly different)
593         // XXX implementations of `scroll'. Figure out where it belongs.
594
595         int direction = static_cast<EvtScroll&>(rEvent).getDirection();
596
597         double percentage = m_rTree.getPositionVar().get();
598         double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
599                                              : m_rTree.visibleItems() );
600         if( direction == EvtScroll::kUp )
601         {
602             percentage += step;
603         }
604         else
605         {
606             percentage -= step;
607         }
608         m_rTree.getPositionVar().set( percentage );
609     }
610
611     /* We changed the nodes, let's fix teh position var */
612     if( bChangedPosition )
613     {
614         VarTree::Iterator it;
615         int i = 0;
616         int iFirst = 0;
617         for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
618              it != m_rTree.end();
619              it = m_flat ? m_rTree.getNextLeaf( it )
620                          : m_rTree.getNextVisibleItem( it ) )
621         {
622             i++;
623             if( it == m_firstPos )
624             {
625                 iFirst = i;
626                 break;
627             }
628         }
629         iFirst += maxItems();
630         if( iFirst >= (m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems()) )
631             iFirst = m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems();
632         float f_new = (float)iFirst / (float)( m_flat ? m_rTree.countLeafs()
633                                                       :m_rTree.visibleItems() );
634         m_dontMove = true;
635         m_rTree.getPositionVar().set( 1.0 - f_new );
636         m_dontMove = false;
637     }
638 }
639
640 bool CtrlTree::mouseOver( int x, int y ) const
641 {
642     const Position *pPos = getPosition();
643     return !pPos ? false :
644         x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight();
645 }
646
647 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h)
648 {
649     const Position *pPos = getPosition();
650     rect region( pPos->getLeft(), pPos->getTop(),
651                  pPos->getWidth(), pPos->getHeight() );
652     rect clip( xDest, yDest, w, h );
653     rect inter;
654
655     if( rect::intersect( region, clip, &inter ) && m_pImage )
656         rImage.drawGraphics( *m_pImage,
657                       inter.x - pPos->getLeft(),
658                       inter.y - pPos->getTop(),
659                       inter.x, inter.y, inter.width, inter.height );
660 }
661
662 bool CtrlTree::ensureVisible( VarTree::Iterator item )
663 {
664     // Find the item to focus
665     int focusItemIndex = 0;
666     VarTree::Iterator it;
667
668     m_rTree.ensureExpanded( item );
669
670     for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
671          it != m_rTree.end();
672          it = m_flat ? m_rTree.getNextLeaf( it )
673                      : m_rTree.getNextVisibleItem( it ) )
674     {
675         if( it->m_id == item->m_id ) break;
676         focusItemIndex++;
677     }
678    return ensureVisible( focusItemIndex );
679 }
680
681 bool CtrlTree::ensureVisible( int focusItemIndex )
682 {
683     // Find  m_firstPos
684     VarTree::Iterator it;
685     int firstPosIndex = 0;
686     for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
687          it != m_rTree.end();
688          it = m_flat ? m_rTree.getNextLeaf( it )
689                      : m_rTree.getNextVisibleItem( it ) )
690     {
691         if( it == m_firstPos ) break;
692         firstPosIndex++;
693     }
694
695     if( it == m_rTree.end() ) return false;
696
697
698     if( it != m_rTree.end()
699         && ( focusItemIndex < firstPosIndex
700            || focusItemIndex > firstPosIndex + maxItems() - 1 ) )
701     {
702         // Scroll to have the wanted stream visible
703         VarPercent &rVarPos = m_rTree.getPositionVar();
704         int indexMax = ( m_flat ? m_rTree.countLeafs()
705                                 : m_rTree.visibleItems() ) - 1;
706         rVarPos.set( 1.0 - (double)focusItemIndex / (double)indexMax );
707         return true;
708     }
709     return false;
710 }
711
712 void CtrlTree::autoScroll()
713 {
714     // Find the current playing stream
715     int playIndex = 0;
716     VarTree::Iterator it;
717
718     for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
719          it != m_rTree.end();
720          it = m_flat ? m_rTree.getNextLeaf( it )
721                      : m_rTree.getNextItem( it ) )
722     {
723         if( it->m_playing )
724         {
725            m_rTree.ensureExpanded( it );
726            break;
727         }
728     }
729
730     for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
731          it != m_rTree.end();
732          it = m_flat ? m_rTree.getNextLeaf( it )
733                      : m_rTree.getNextVisibleItem( it ) )
734     {
735         if( it->m_playing )
736         {
737            ensureVisible( playIndex );
738            break;
739         }
740         playIndex++;
741     }
742 }
743
744
745 void CtrlTree::makeImage()
746 {
747     stats_TimerStart( getIntf(), "[Skins] Playlist image",
748                       STATS_TIMER_SKINS_PLAYTREE_IMAGE );
749     delete m_pImage;
750
751     // Get the size of the control
752     const Position *pPos = getPosition();
753     if( !pPos )
754     {
755         stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
756         return;
757     }
758     int width = pPos->getWidth();
759     int height = pPos->getHeight();
760
761     int i_itemHeight = itemHeight();
762
763     // Create an image
764     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
765     m_pImage = pOsFactory->createOSGraphics( width, height );
766
767     VarTree::Iterator it = m_firstPos;
768
769     if( m_pBgBitmap )
770     {
771         // Draw the background bitmap
772         ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
773         m_pImage->drawBitmap( bmp, 0, 0 );
774
775         for( int yPos = 0; yPos < height; yPos += i_itemHeight )
776         {
777             if( it != m_rTree.end() )
778             {
779                 if( (*it).m_selected )
780                 {
781                     int rectHeight = __MIN( i_itemHeight, height - yPos );
782                     m_pImage->fillRect( 0, yPos, width, rectHeight,
783                                         m_selColor );
784                 }
785                 do
786                 {
787                     it = m_flat ? m_rTree.getNextLeaf( it )
788                                 : m_rTree.getNextVisibleItem( it );
789                 } while( it != m_rTree.end() && it->m_deleted );
790             }
791         }
792     }
793     else
794     {
795         // FIXME (TRYME)
796         // Fill background with background color
797         uint32_t bgColor = m_bgColor1;
798         m_pImage->fillRect( 0, 0, width, height, bgColor );
799         for( int yPos = 0; yPos < height; yPos += i_itemHeight )
800         {
801             int rectHeight = __MIN( i_itemHeight, height - yPos );
802             if( it == m_rTree.end() )
803                 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
804             else
805             {
806                 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
807                 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
808                 do
809                 {
810                     it = m_flat ? m_rTree.getNextLeaf( it )
811                                 : m_rTree.getNextVisibleItem( it );
812                 } while( it != m_rTree.end() && it->m_deleted );
813             }
814             bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
815         }
816     }
817
818     int bitmapWidth = itemImageWidth();
819
820     int yPos = 0;
821     it = m_firstPos;
822     while( it != m_rTree.end() && yPos < height )
823     {
824         const GenericBitmap *m_pCurBitmap;
825         UString *pStr = (UString*)(it->m_cString.get());
826         uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
827
828         // Draw the text
829         if( pStr != NULL )
830         {
831             int depth = m_flat ? 1 : it->depth();
832             GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
833             if( !pText )
834             {
835                 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
836                 return;
837             }
838             if( it->size() )
839                 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
840             else
841                 m_pCurBitmap = m_pItemBitmap;
842
843             if( m_pCurBitmap )
844             {
845                 // Make sure we are centered on the line
846                 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
847                 if( yPos2 >= height )
848                 {
849                     delete pText;
850                     break;
851                 }
852                 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
853                                       bitmapWidth * (depth - 1 ), yPos2,
854                                       m_pCurBitmap->getWidth(),
855                                       __MIN( m_pCurBitmap->getHeight(),
856                                              height -  yPos2), true );
857             }
858             yPos += i_itemHeight - pText->getHeight();
859             int ySrc = 0;
860             if( yPos < 0 )
861             {
862                 ySrc = - yPos;
863                 yPos = 0;
864             }
865             int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
866             m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
867                                   pText->getWidth(),
868                                   lineHeight, true );
869             yPos += (pText->getHeight() - ySrc );
870             delete pText;
871         }
872         do
873         {
874             it = m_flat ? m_rTree.getNextLeaf( it )
875                 : m_rTree.getNextVisibleItem( it );
876         } while( it != m_rTree.end() && it->m_deleted );
877     }
878     stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
879 }
880
881 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
882 {
883     // The first item is m_firstPos.
884     // We decrement pos as we try the other items, until pos == 0.
885     VarTree::Iterator it;
886     for( it = m_firstPos; it != m_rTree.end() && pos != 0;
887          it = m_flat ? m_rTree.getNextLeaf( it )
888                      : m_rTree.getNextVisibleItem( it ) )
889     {
890         pos--;
891     }
892
893     return it;
894 }