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