1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
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"
40 #define SCROLL_STEP 0.05
41 #define LINE_INTERVAL 1 // Number of pixels inserted between 2 lines
44 CtrlTree::CtrlTree( intf_thread_t *pIntf,
46 const GenericFont &rFont,
47 const GenericBitmap *pBgBitmap,
48 const GenericBitmap *pItemBitmap,
49 const GenericBitmap *pOpenBitmap,
50 const GenericBitmap *pClosedBitmap,
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 )
66 // Observe the tree and position variables
67 m_rTree.addObserver( this );
68 m_rTree.getPositionVar().addObserver( this );
70 m_flat = pFlat->get();
72 m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
79 m_rTree.getPositionVar().delObserver( this );
80 m_rTree.delObserver( this );
84 int CtrlTree::itemHeight()
86 int itemHeight = m_rFont.getSize();
91 itemHeight = __MAX( m_pClosedBitmap->getHeight(), itemHeight );
95 itemHeight = __MAX( m_pOpenBitmap->getHeight(), itemHeight );
100 itemHeight = __MAX( m_pItemBitmap->getHeight(), itemHeight );
102 itemHeight += LINE_INTERVAL;
106 int CtrlTree::itemImageWidth()
111 if( m_pClosedBitmap )
113 bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
117 bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
122 bitmapWidth = __MAX( m_pItemBitmap->getWidth(), bitmapWidth );
124 return bitmapWidth + 2;
127 int CtrlTree::maxItems()
129 const Position *pPos = getPosition();
134 return pPos->getHeight() / itemHeight();
138 void CtrlTree::onUpdate( Subject<VarTree, tree_update> &rTree,
141 if( arg->i_type == 0 ) // Item update
143 if( arg->b_active_item )
146 ///\todo We should make image if we are visible in the view
150 /// \todo handle delete in a more clever way
151 else if ( arg->i_type == 1 ) // Global change or deletion
153 m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
156 else if ( arg->i_type == 2 ) // Item-append
158 if( m_flat && m_firstPos->size() )
159 m_firstPos = m_rTree.getNextLeaf( m_firstPos );
160 /// \todo Check if the item is really visible in the view
161 // (we only check if it in the document)
162 if( arg->b_visible == true )
167 else if( arg->i_type == 3 ) // item-del
169 /* Make sure firstPos and lastSelected are still valid */
170 while( m_firstPos->m_deleted && m_firstPos != m_rTree.root()->begin() )
172 m_firstPos = m_flat ? m_rTree.getPrevLeaf( m_firstPos )
173 : m_rTree.getPrevVisibleItem( m_firstPos );
175 if( m_firstPos->m_deleted )
176 m_firstPos = m_flat ? m_rTree.firstLeaf()
177 : m_rTree.root()->begin();
179 if( arg->b_visible == true )
187 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent, void* arg)
189 // Determine what is the first item to display
190 VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
192 if( m_dontMove ) return;
196 excessItems = m_rTree.countLeafs() - maxItems();
198 excessItems = m_rTree.visibleItems() - maxItems();
202 VarPercent &rVarPos = m_rTree.getPositionVar();
203 // a simple (int)(...) causes rounding errors !
208 it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
210 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
212 if( m_firstPos != it )
214 // Redraw the control if the position has changed
221 void CtrlTree::onResize()
223 // Determine what is the first item to display
224 VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
228 excessItems = m_rTree.countLeafs() - maxItems();
230 excessItems = m_rTree.visibleItems() - maxItems();
234 VarPercent &rVarPos = m_rTree.getPositionVar();
235 // a simple (int)(...) causes rounding errors !
240 it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
242 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
244 // Redraw the control if the position has changed
249 void CtrlTree::onPositionChange()
254 void CtrlTree::handleEvent( EvtGeneric &rEvent )
256 bool bChangedPosition = false;
257 VarTree::Iterator toShow; bool needShow = false;
258 if( rEvent.getAsString().find( "key:down" ) != string::npos )
260 int key = ((EvtKey&)rEvent).getKey();
261 VarTree::Iterator it;
262 bool previousWasSelected = false;
264 /* Delete the selection */
265 if( key == KEY_DELETE )
267 /* Find first non selected item before m_pLastSelected */
268 VarTree::Iterator it_sel = m_flat ? m_rTree.firstLeaf()
270 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
272 it = m_flat ? m_rTree.getNextLeaf( it )
273 : m_rTree.getNextVisibleItem( it ) )
275 if( &*it == m_pLastSelected ) break;
276 if( !it->m_selected ) it_sel = it;
279 /* Delete selected stuff */
280 m_rTree.delSelected();
283 it_sel->m_selected = true;
284 m_pLastSelected = &*it_sel;
286 else if( key == KEY_PAGEDOWN )
289 int i = (int)(maxItems()*1.5);
292 VarTree::Iterator it_old = it;
293 it = m_flat ? m_rTree.getNextLeaf( it )
294 : m_rTree.getNextVisibleItem( it );
295 /* End is already visible, dont' scroll */
296 if( it == m_rTree.end() )
312 else if (key == KEY_PAGEUP )
316 while( i >= maxItems()/2 )
318 it = m_flat ? m_rTree.getPrevLeaf( it )
319 : m_rTree.getPrevVisibleItem( it );
320 /* End is already visible, dont' scroll */
321 if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
334 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
336 it = m_flat ? m_rTree.getNextLeaf( it )
337 : m_rTree.getNextVisibleItem( it ) )
339 VarTree::Iterator next = m_flat ? m_rTree.getNextLeaf( it )
340 : m_rTree.getNextVisibleItem( it );
343 // Scroll up one item
345 && it != it->parent()->begin() )
346 || &*it != m_pLastSelected )
348 bool nextWasSelected = ( &*next == m_pLastSelected );
349 it->m_selected = nextWasSelected;
350 if( nextWasSelected )
352 m_pLastSelected = &*it;
353 needShow = true; toShow = it;
357 else if( key == KEY_DOWN )
359 // Scroll down one item
361 && next != it->parent()->end() )
362 || &*it != m_pLastSelected )
364 (*it).m_selected = previousWasSelected;
366 if( previousWasSelected )
368 m_pLastSelected = &*it;
369 needShow = true; toShow = it;
370 previousWasSelected = false;
374 previousWasSelected = ( &*it == m_pLastSelected );
377 // Fix last tree item selection
378 if( ( m_flat ? m_rTree.getNextLeaf( it )
379 : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
380 && &*it == m_pLastSelected )
382 (*it).m_selected = true;
385 else if( key == KEY_RIGHT )
387 // Go down one level (and expand node)
388 if( &*it == m_pLastSelected )
394 it->m_selected = false;
395 it->begin()->m_selected = true;
396 m_pLastSelected = &*(it->begin());
400 m_rTree.action( &*it );
405 it->m_expanded = true;
406 bChangedPosition = true;
410 else if( key == KEY_LEFT )
412 // Go up one level (and close node)
413 if( &*it == m_pLastSelected )
415 if( it->m_expanded && it->size() )
417 it->m_expanded = false;
418 bChangedPosition = true;
422 if( it->parent() && it->parent() != &m_rTree)
424 it->m_selected = false;
425 m_pLastSelected = it->parent();
426 m_pLastSelected->m_selected = true;
431 else if( key == KEY_ENTER || key == ' ' )
433 // Go up one level (and close node)
434 if( &*it == m_pLastSelected )
436 m_rTree.action( &*it );
441 ensureVisible( toShow );
443 // Redraw the control
448 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
450 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
451 const Position *pos = getPosition();
452 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
453 int xPos = rEvtMouse.getXPos() - pos->getLeft();
454 VarTree::Iterator it;
456 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
459 VarTree::Iterator itClicked = findItemAtPos( yPos );
460 // Flag to know if the current item must be selected
462 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
464 it = m_flat ? m_rTree.getNextLeaf( it )
465 : m_rTree.getNextVisibleItem( it ) )
467 bool nextSelect = select;
468 if( it == itClicked || &*it == m_pLastSelected )
480 it->m_selected = (*it).m_selected || select;
484 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
487 // Invert the selection of the item
488 it = findItemAtPos( yPos );
489 if( it != m_rTree.end() )
491 it->m_selected = !it->m_selected;
492 m_pLastSelected = &*it;
495 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
498 VarTree::Iterator itClicked = findItemAtPos( yPos );
499 // Flag to know if the current item must be selected
501 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
503 it = m_flat ? m_rTree.getNextLeaf( it )
504 : m_rTree.getNextVisibleItem( it ) )
506 bool nextSelect = select;
507 if( it == itClicked || &*it == m_pLastSelected )
519 it->m_selected = select;
523 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
526 it = findItemAtPos(yPos);
527 if( it != m_rTree.end() )
529 if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
530 && xPos < it->depth() * itemImageWidth() )
533 // Fold/unfold the item
534 it->m_expanded = !it->m_expanded;
535 bChangedPosition = true;
539 // Unselect any previously selected item
540 VarTree::Iterator it2;
541 for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
542 it2 != m_rTree.end();
543 it2 = m_flat ? m_rTree.getNextLeaf( it2 )
544 : m_rTree.getNextVisibleItem( it2 ) )
546 it2->m_selected = false;
548 // Select the new item
549 if( it != m_rTree.end() )
551 it->m_selected = true;
552 m_pLastSelected = &*it;
558 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
561 it = findItemAtPos(yPos);
562 if( it != m_rTree.end() )
564 // Execute the action associated to this item
565 m_rTree.action( &*it );
568 // Redraw the control
573 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
575 // XXX ctrl_slider.cpp has two more (but slightly different)
576 // XXX implementations of `scroll'. Figure out where it belongs.
578 int direction = static_cast<EvtScroll&>(rEvent).getDirection();
580 double percentage = m_rTree.getPositionVar().get();
581 double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
582 : m_rTree.visibleItems() );
583 if( direction == EvtScroll::kUp )
591 m_rTree.getPositionVar().set( percentage );
594 /* We changed the nodes, let's fix teh position var */
595 if( bChangedPosition )
597 VarTree::Iterator it;
600 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
602 it = m_flat ? m_rTree.getNextLeaf( it )
603 : m_rTree.getNextVisibleItem( it ) )
606 if( it == m_firstPos )
612 iFirst += maxItems();
613 if( iFirst >= m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems() )
614 iFirst = m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems();
615 float f_new = (float)iFirst / (float)( m_flat ? m_rTree.countLeafs()
616 :m_rTree.visibleItems() );
618 m_rTree.getPositionVar().set( 1.0 - f_new );
623 bool CtrlTree::mouseOver( int x, int y ) const
625 const Position *pPos = getPosition();
626 return !pPos ? false :
627 x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight();
630 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
633 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
636 bool CtrlTree::ensureVisible( VarTree::Iterator item )
638 // Find the item to focus
639 int focusItemIndex = 0;
640 VarTree::Iterator it;
642 m_rTree.ensureExpanded( item );
644 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
646 it = m_flat ? m_rTree.getNextLeaf( it )
647 : m_rTree.getNextVisibleItem( it ) )
649 if( it->m_id == item->m_id ) break;
652 return ensureVisible( focusItemIndex );
655 bool CtrlTree::ensureVisible( int focusItemIndex )
658 VarTree::Iterator it;
659 int firstPosIndex = 0;
660 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
662 it = m_flat ? m_rTree.getNextLeaf( it )
663 : m_rTree.getNextVisibleItem( it ) )
665 if( it == m_firstPos ) break;
669 if( it == m_rTree.end() ) return false;
672 if( it != m_rTree.end()
673 && ( focusItemIndex < firstPosIndex
674 || focusItemIndex > firstPosIndex + maxItems() ) )
676 // Scroll to have the wanted stream visible
677 VarPercent &rVarPos = m_rTree.getPositionVar();
678 rVarPos.set( 1.0 - (double)focusItemIndex /
679 (double)( m_flat ? m_rTree.countLeafs()
680 : m_rTree.visibleItems() ) );
686 void CtrlTree::autoScroll()
688 // Find the current playing stream
690 VarTree::Iterator it;
692 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
694 it = m_flat ? m_rTree.getNextLeaf( it )
695 : m_rTree.getNextItem( it ) )
699 m_rTree.ensureExpanded( it );
703 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
705 it = m_flat ? m_rTree.getNextLeaf( it )
706 : m_rTree.getNextVisibleItem( it ) )
713 if( it == m_rTree.end() ) return;
716 ensureVisible( playIndex );
720 void CtrlTree::makeImage()
722 stats_TimerStart( getIntf(), "[Skins] Playlist image",
723 STATS_TIMER_SKINS_PLAYTREE_IMAGE );
726 // Get the size of the control
727 const Position *pPos = getPosition();
730 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
733 int width = pPos->getWidth();
734 int height = pPos->getHeight();
736 int i_itemHeight = itemHeight();
739 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
740 m_pImage = pOsFactory->createOSGraphics( width, height );
742 VarTree::Iterator it = m_firstPos;
746 // Draw the background bitmap
747 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
748 m_pImage->drawBitmap( bmp, 0, 0 );
750 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
752 if( it != m_rTree.end() )
754 if( (*it).m_selected )
756 int rectHeight = __MIN( i_itemHeight, height - yPos );
757 m_pImage->fillRect( 0, yPos, width, rectHeight,
762 it = m_flat ? m_rTree.getNextLeaf( it )
763 : m_rTree.getNextVisibleItem( it );
764 } while( it != m_rTree.end() && it->m_deleted );
771 // Fill background with background color
772 uint32_t bgColor = m_bgColor1;
773 m_pImage->fillRect( 0, 0, width, height, bgColor );
774 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
776 int rectHeight = __MIN( i_itemHeight, height - yPos );
777 if( it == m_rTree.end() )
778 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
781 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
782 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
785 it = m_flat ? m_rTree.getNextLeaf( it )
786 : m_rTree.getNextVisibleItem( it );
787 } while( it != m_rTree.end() && it->m_deleted );
789 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
793 int bitmapWidth = itemImageWidth();
797 while( it != m_rTree.end() && yPos < height )
799 const GenericBitmap *m_pCurBitmap;
800 UString *pStr = (UString*)(it->m_cString.get());
801 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
806 int depth = m_flat ? 1 : it->depth();
807 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
810 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
814 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
816 m_pCurBitmap = m_pItemBitmap;
820 // Make sure we are centered on the line
821 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
822 if( yPos2 >= height )
827 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
828 bitmapWidth * (depth - 1 ), yPos2,
829 m_pCurBitmap->getWidth(),
830 __MIN( m_pCurBitmap->getHeight(),
831 height - yPos2), true );
833 yPos += i_itemHeight - pText->getHeight();
840 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
841 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
844 yPos += (pText->getHeight() - ySrc );
849 it = m_flat ? m_rTree.getNextLeaf( it )
850 : m_rTree.getNextVisibleItem( it );
851 } while( it != m_rTree.end() && it->m_deleted );
853 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
856 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
858 // The first item is m_firstPos.
859 // We decrement pos as we try the other items, until pos == 0.
860 VarTree::Iterator it;
861 for( it = m_firstPos; it != m_rTree.end() && pos != 0;
862 it = m_flat ? m_rTree.getNextLeaf( it )
863 : m_rTree.getNextVisibleItem( it ) )