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 ),
65 m_pScaledBitmap( NULL )
67 // Observe the tree and position variables
68 m_rTree.addObserver( this );
69 m_rTree.getPositionVar().addObserver( this );
71 m_flat = pFlat->get();
73 m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
80 m_rTree.getPositionVar().delObserver( this );
81 m_rTree.delObserver( this );
82 delete m_pScaledBitmap;
86 int CtrlTree::itemHeight()
88 int itemHeight = m_rFont.getSize();
93 itemHeight = __MAX( m_pClosedBitmap->getHeight(), itemHeight );
97 itemHeight = __MAX( m_pOpenBitmap->getHeight(), itemHeight );
102 itemHeight = __MAX( m_pItemBitmap->getHeight(), itemHeight );
104 itemHeight += LINE_INTERVAL;
108 int CtrlTree::itemImageWidth()
113 if( m_pClosedBitmap )
115 bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
119 bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
124 bitmapWidth = __MAX( m_pItemBitmap->getWidth(), bitmapWidth );
126 return bitmapWidth + 2;
129 int CtrlTree::maxItems()
131 const Position *pPos = getPosition();
136 return pPos->getHeight() / itemHeight();
140 void CtrlTree::onUpdate( Subject<VarTree, tree_update> &rTree,
143 if( arg->i_type == 0 ) // Item update
145 if( arg->b_active_item )
150 else if ( arg->i_type == 1 ) // Global change or deletion
152 m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
157 else if ( arg->i_type == 2 ) // Item-append
159 if( m_flat && m_firstPos->size() )
161 m_firstPos = m_rTree.getNextLeaf( m_firstPos );
166 else if( isItemVisible( arg->i_id ) )
172 else if( arg->i_type == 3 ) // item-del
174 /* Make sure firstPos is valid */
175 VarTree::Iterator it_old = m_firstPos;
176 while( m_firstPos->isDeleted() &&
177 m_firstPos != (m_flat ? m_rTree.firstLeaf()
180 m_firstPos = m_flat ? m_rTree.getPrevLeaf( m_firstPos )
181 : m_rTree.getPrevVisibleItem( m_firstPos );
183 if( m_firstPos->isDeleted() )
184 m_firstPos = m_rTree.begin();
186 if( m_firstPos != it_old || isItemVisible( arg->i_id ) )
194 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent, void* arg)
196 // Determine what is the first item to display
197 VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
199 if( m_dontMove ) return;
203 excessItems = m_rTree.countLeafs() - maxItems();
205 excessItems = m_rTree.visibleItems() - maxItems();
209 VarPercent &rVarPos = m_rTree.getPositionVar();
210 // a simple (int)(...) causes rounding errors !
215 it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
217 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
219 if( m_firstPos != it )
221 // Redraw the control if the position has changed
228 void CtrlTree::onResize()
230 // Determine what is the first item to display
231 VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
235 excessItems = m_rTree.countLeafs() - maxItems();
237 excessItems = m_rTree.visibleItems() - maxItems();
241 VarPercent &rVarPos = m_rTree.getPositionVar();
242 // a simple (int)(...) causes rounding errors !
247 it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
249 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
251 // Redraw the control if the position has changed
256 void CtrlTree::onPositionChange()
261 void CtrlTree::handleEvent( EvtGeneric &rEvent )
263 bool bChangedPosition = false;
264 VarTree::Iterator toShow; bool needShow = false;
265 if( rEvent.getAsString().find( "key:down" ) != string::npos )
267 int key = ((EvtKey&)rEvent).getKey();
268 VarTree::Iterator it;
269 bool previousWasSelected = false;
271 /* Delete the selection */
272 if( key == KEY_DELETE )
274 /* Find first non selected item before m_pLastSelected */
275 VarTree::Iterator it_sel = m_flat ? m_rTree.firstLeaf()
277 for( it = (m_flat ? m_rTree.firstLeaf() : m_rTree.begin());
279 it = (m_flat ? m_rTree.getNextLeaf( it )
280 : m_rTree.getNextVisibleItem( it )) )
282 if( &*it == m_pLastSelected ) break;
283 if( !it->isSelected() ) it_sel = it;
286 /* Delete selected stuff */
287 m_rTree.delSelected();
289 /* Verify if there is still sthg selected (e.g read-only items) */
290 m_pLastSelected = NULL;
291 for( it = (m_flat ? m_rTree.firstLeaf() : m_rTree.begin());
293 it = (m_flat ? m_rTree.getNextLeaf( it )
294 : m_rTree.getNextVisibleItem( it )) )
296 if( it->isSelected() )
297 m_pLastSelected = &*it;
300 /* if everything was deleted, use it_sel as last selection */
301 if( !m_pLastSelected )
303 it_sel->setSelected( true );
304 m_pLastSelected = &*it_sel;
307 // Redraw the control
311 else if( key == KEY_PAGEDOWN )
314 int i = (int)(maxItems()*1.5);
317 VarTree::Iterator it_old = it;
318 it = m_flat ? m_rTree.getNextLeaf( it )
319 : m_rTree.getNextVisibleItem( it );
320 /* End is already visible, dont' scroll */
321 if( it == m_rTree.end() )
336 else if (key == KEY_PAGEUP )
340 while( i >= maxItems()/2 )
342 it = m_flat ? m_rTree.getPrevLeaf( it )
343 : m_rTree.getPrevVisibleItem( it );
344 /* End is already visible, dont' scroll */
345 if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
355 else if ( key == KEY_UP ||
362 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
364 it = m_flat ? m_rTree.getNextLeaf( it )
365 : m_rTree.getNextVisibleItem( it ) )
367 VarTree::Iterator next = m_flat ?
368 m_rTree.getNextLeaf( it ) :
369 m_rTree.getNextVisibleItem( it );
372 // Scroll up one item
374 && it != it->parent()->begin() )
375 || &*it != m_pLastSelected )
377 bool nextWasSelected = ( &*next == m_pLastSelected );
378 it->setSelected( nextWasSelected );
379 if( nextWasSelected )
381 m_pLastSelected = &*it;
382 needShow = true; toShow = it;
386 else if( key == KEY_DOWN )
388 // Scroll down one item
390 && next != it->parent()->end() )
391 || &*it != m_pLastSelected )
393 it->setSelected( previousWasSelected );
395 if( previousWasSelected )
397 m_pLastSelected = &*it;
398 needShow = true; toShow = it;
399 previousWasSelected = false;
403 previousWasSelected = ( &*it == m_pLastSelected );
406 // Fix last tree item selection
407 if( ( m_flat ? m_rTree.getNextLeaf( it )
408 : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
409 && &*it == m_pLastSelected )
411 it->setSelected( true );
414 else if( key == KEY_RIGHT )
416 // Go down one level (and expand node)
417 if( &*it == m_pLastSelected )
419 if( it->isExpanded() )
423 it->setSelected( false );
424 it->begin()->setSelected( true );
425 m_pLastSelected = &*(it->begin());
429 m_rTree.action( &*it );
434 it->setExpanded( true );
435 bChangedPosition = true;
439 else if( key == KEY_LEFT )
441 // Go up one level (and close node)
442 if( &*it == m_pLastSelected )
444 if( it->isExpanded() && it->size() )
446 it->setExpanded( false );
447 bChangedPosition = true;
451 if( it->parent() && it->parent() != &m_rTree)
453 it->setSelected( false );
454 m_pLastSelected = it->parent();
455 m_pLastSelected->setSelected( true );
460 else if( key == KEY_ENTER || key == ' ' )
462 // Go up one level (and close node)
463 if( &*it == m_pLastSelected )
465 m_rTree.action( &*it );
470 ensureVisible( toShow );
471 // Redraw the control
477 // other keys to be forwarded to vlc core
478 EvtKey& rEvtKey = (EvtKey&)rEvent;
479 var_SetInteger( getIntf()->p_libvlc, "key-pressed",
480 rEvtKey.getModKey() );
485 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
487 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
488 const Position *pos = getPosition();
489 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
490 int xPos = rEvtMouse.getXPos() - pos->getLeft();
491 VarTree::Iterator it;
493 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
496 VarTree::Iterator itClicked = findItemAtPos( yPos );
497 // Flag to know if the current item must be selected
499 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
501 it = m_flat ? m_rTree.getNextLeaf( it )
502 : m_rTree.getNextVisibleItem( it ) )
504 bool nextSelect = select;
505 if( it == itClicked || &*it == m_pLastSelected )
517 it->setSelected( it->isSelected() || select );
520 // Redraw the control
524 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
527 // Invert the selection of the item
528 it = findItemAtPos( yPos );
529 if( it != m_rTree.end() )
531 it->toggleSelected();
532 m_pLastSelected = &*it;
534 // Redraw the control
538 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
541 VarTree::Iterator itClicked = findItemAtPos( yPos );
542 // Flag to know if the current item must be selected
544 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
546 it = m_flat ? m_rTree.getNextLeaf( it )
547 : m_rTree.getNextVisibleItem( it ) )
549 bool nextSelect = select;
550 if( it == itClicked || &*it == m_pLastSelected )
562 it->setSelected( select );
565 // Redraw the control
569 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
572 it = findItemAtPos(yPos);
573 if( it != m_rTree.end() )
575 if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
576 && xPos < it->depth() * itemImageWidth() )
579 // Fold/unfold the item
580 it->toggleExpanded();
581 bChangedPosition = true;
585 // Unselect any previously selected item
586 VarTree::Iterator it2;
587 for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
588 it2 != m_rTree.end();
589 it2 = m_flat ? m_rTree.getNextLeaf( it2 )
590 : m_rTree.getNextVisibleItem( it2 ) )
592 it2->setSelected( false );
594 // Select the new item
595 if( it != m_rTree.end() )
597 it->setSelected( true );
598 m_pLastSelected = &*it;
602 // Redraw the control
606 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
609 it = findItemAtPos(yPos);
610 if( it != m_rTree.end() )
612 // Execute the action associated to this item
613 m_rTree.action( &*it );
615 // Redraw the control
621 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
623 // XXX ctrl_slider.cpp has two more (but slightly different)
624 // XXX implementations of `scroll'. Figure out where it belongs.
626 int direction = static_cast<EvtScroll&>(rEvent).getDirection();
628 double percentage = m_rTree.getPositionVar().get();
629 double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
630 : m_rTree.visibleItems() );
631 if( direction == EvtScroll::kUp )
639 m_rTree.getPositionVar().set( percentage );
642 /* We changed the nodes, let's fix the position var */
643 if( bChangedPosition )
645 VarTree::Iterator it;
647 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
649 it = m_flat ? m_rTree.getNextLeaf( it )
650 : m_rTree.getNextVisibleItem( it ) )
652 if( it == m_firstPos )
657 int indexMax = ( m_flat ? m_rTree.countLeafs()
658 : m_rTree.visibleItems() ) - 1;
659 float f_new = (float)iFirst / (float)indexMax;
662 m_rTree.getPositionVar().set( 1.0 - f_new );
667 bool CtrlTree::mouseOver( int x, int y ) const
669 const Position *pPos = getPosition();
670 return !pPos ? false :
671 x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight();
674 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h)
676 const Position *pPos = getPosition();
677 rect region( pPos->getLeft(), pPos->getTop(),
678 pPos->getWidth(), pPos->getHeight() );
679 rect clip( xDest, yDest, w, h );
682 if( rect::intersect( region, clip, &inter ) && m_pImage )
683 rImage.drawGraphics( *m_pImage,
684 inter.x - pPos->getLeft(),
685 inter.y - pPos->getTop(),
686 inter.x, inter.y, inter.width, inter.height );
689 bool CtrlTree::ensureVisible( VarTree::Iterator item )
691 m_rTree.ensureExpanded( item );
693 int firstPosIndex = m_rTree.getRank( m_firstPos, m_flat) - 1;
694 int focusItemIndex = m_rTree.getRank( item, m_flat) - 1;
696 if( focusItemIndex < firstPosIndex ||
697 focusItemIndex > firstPosIndex + maxItems() - 1 )
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 );
709 void CtrlTree::autoScroll()
711 // Find the current playing stream
713 VarTree::Iterator it;
715 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
717 it = m_flat ? m_rTree.getNextLeaf( it )
718 : m_rTree.getNextItem( it ) )
720 if( it->isPlaying() )
729 void CtrlTree::makeImage()
731 stats_TimerStart( getIntf(), "[Skins] Playlist image",
732 STATS_TIMER_SKINS_PLAYTREE_IMAGE );
735 // Get the size of the control
736 const Position *pPos = getPosition();
739 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
742 int width = pPos->getWidth();
743 int height = pPos->getHeight();
745 int i_itemHeight = itemHeight();
748 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
749 m_pImage = pOsFactory->createOSGraphics( width, height );
751 VarTree::Iterator it = m_firstPos;
755 // Draw the background bitmap
756 if( !m_pScaledBitmap ||
757 m_pScaledBitmap->getWidth() != width ||
758 m_pScaledBitmap->getHeight() != height )
760 delete m_pScaledBitmap;
762 new ScaledBitmap( getIntf(), *m_pBgBitmap, width, height );
764 m_pImage->drawBitmap( *m_pScaledBitmap, 0, 0 );
766 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
768 if( it != m_rTree.end() )
770 if( it->isSelected() )
772 int rectHeight = __MIN( i_itemHeight, height - yPos );
773 m_pImage->fillRect( 0, yPos, width, rectHeight,
778 it = m_flat ? m_rTree.getNextLeaf( it )
779 : m_rTree.getNextVisibleItem( it );
780 } while( it != m_rTree.end() && it->isDeleted() );
787 // Fill background with background color
788 uint32_t bgColor = m_bgColor1;
789 m_pImage->fillRect( 0, 0, width, height, bgColor );
790 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
792 int rectHeight = __MIN( i_itemHeight, height - yPos );
793 if( it == m_rTree.end() )
794 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
797 uint32_t color = ( it->isSelected() ? m_selColor : bgColor );
798 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
801 it = m_flat ? m_rTree.getNextLeaf( it )
802 : m_rTree.getNextVisibleItem( it );
803 } while( it != m_rTree.end() && it->isDeleted() );
805 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
809 int bitmapWidth = itemImageWidth();
813 while( it != m_rTree.end() && yPos < height )
815 const GenericBitmap *m_pCurBitmap;
816 UString *pStr = it->getString();
817 uint32_t color = ( it->isPlaying() ? m_playColor : m_fgColor );
822 int depth = m_flat ? 1 : it->depth();
823 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
826 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
830 m_pCurBitmap = it->isExpanded() ? m_pOpenBitmap : m_pClosedBitmap;
832 m_pCurBitmap = m_pItemBitmap;
836 // Make sure we are centered on the line
837 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
838 if( yPos2 >= height )
843 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
844 bitmapWidth * (depth - 1 ), yPos2,
845 m_pCurBitmap->getWidth(),
846 __MIN( m_pCurBitmap->getHeight(),
847 height - yPos2), true );
849 yPos += i_itemHeight - pText->getHeight();
856 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
857 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
860 yPos += (pText->getHeight() - ySrc );
865 it = m_flat ? m_rTree.getNextLeaf( it )
866 : m_rTree.getNextVisibleItem( it );
867 } while( it != m_rTree.end() && it->isDeleted() );
869 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
872 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
874 // The first item is m_firstPos.
875 // We decrement pos as we try the other items, until pos == 0.
876 VarTree::Iterator it;
877 for( it = m_firstPos; it != m_rTree.end() && pos != 0;
878 it = m_flat ? m_rTree.getNextLeaf( it )
879 : m_rTree.getNextVisibleItem( it ) )
887 bool CtrlTree::isItemVisible( int id )
889 VarTree::Iterator it = m_rTree.findById( id );
891 int rank1 = m_rTree.getRank( m_firstPos, m_flat );
892 int rank2 = m_rTree.getRank( it, m_flat );
893 return ( rank2 >= rank1 && rank2 <= rank1 + maxItems() -1 );