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