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
250 void CtrlTree::onPositionChange()
256 void CtrlTree::handleEvent( EvtGeneric &rEvent )
258 bool bChangedPosition = false;
259 VarTree::Iterator toShow; bool needShow = false;
260 if( rEvent.getAsString().find( "key:down" ) != string::npos )
262 int key = ((EvtKey&)rEvent).getKey();
263 VarTree::Iterator it;
264 bool previousWasSelected = false;
266 /* Delete the selection */
267 if( key == KEY_DELETE )
269 /* Find first non selected item before m_pLastSelected */
270 VarTree::Iterator it_sel = m_flat ? m_rTree.firstLeaf()
272 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
274 it = m_flat ? m_rTree.getNextLeaf( it )
275 : m_rTree.getNextVisibleItem( it ) )
277 if( &*it == m_pLastSelected ) break;
278 if( !it->m_selected ) it_sel = it;
281 /* Delete selected stuff */
282 m_rTree.delSelected();
285 it_sel->m_selected = true;
286 m_pLastSelected = &*it_sel;
288 else if( key == KEY_PAGEDOWN )
291 int i = (int)(maxItems()*1.5);
294 VarTree::Iterator it_old = it;
295 it = m_flat ? m_rTree.getNextLeaf( it )
296 : m_rTree.getNextVisibleItem( it );
297 /* End is already visible, dont' scroll */
298 if( it == m_rTree.end() )
314 else if (key == KEY_PAGEUP )
318 while( i >= maxItems()/2 )
320 it = m_flat ? m_rTree.getPrevLeaf( it )
321 : m_rTree.getPrevVisibleItem( it );
322 /* End is already visible, dont' scroll */
323 if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
336 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
338 it = m_flat ? m_rTree.getNextLeaf( it )
339 : m_rTree.getNextVisibleItem( it ) )
341 VarTree::Iterator next = m_flat ? m_rTree.getNextLeaf( it )
342 : m_rTree.getNextVisibleItem( it );
345 // Scroll up one item
347 && it != it->parent()->begin() )
348 || &*it != m_pLastSelected )
350 bool nextWasSelected = ( &*next == m_pLastSelected );
351 it->m_selected = nextWasSelected;
352 if( nextWasSelected )
354 m_pLastSelected = &*it;
355 needShow = true; toShow = it;
359 else if( key == KEY_DOWN )
361 // Scroll down one item
363 && next != it->parent()->end() )
364 || &*it != m_pLastSelected )
366 (*it).m_selected = previousWasSelected;
368 if( previousWasSelected )
370 m_pLastSelected = &*it;
371 needShow = true; toShow = it;
372 previousWasSelected = false;
376 previousWasSelected = ( &*it == m_pLastSelected );
379 // Fix last tree item selection
380 if( ( m_flat ? m_rTree.getNextLeaf( it )
381 : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
382 && &*it == m_pLastSelected )
384 (*it).m_selected = true;
387 else if( key == KEY_RIGHT )
389 // Go down one level (and expand node)
390 if( &*it == m_pLastSelected )
396 it->m_selected = false;
397 it->begin()->m_selected = true;
398 m_pLastSelected = &*(it->begin());
402 m_rTree.action( &*it );
407 it->m_expanded = true;
408 bChangedPosition = true;
412 else if( key == KEY_LEFT )
414 // Go up one level (and close node)
415 if( &*it == m_pLastSelected )
417 if( it->m_expanded && it->size() )
419 it->m_expanded = false;
420 bChangedPosition = true;
424 if( it->parent() && it->parent() != &m_rTree)
426 it->m_selected = false;
427 m_pLastSelected = it->parent();
428 m_pLastSelected->m_selected = true;
433 else if( key == KEY_ENTER || key == ' ' )
435 // Go up one level (and close node)
436 if( &*it == m_pLastSelected )
438 m_rTree.action( &*it );
443 ensureVisible( toShow );
445 // Redraw the control
450 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
452 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
453 const Position *pos = getPosition();
454 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
455 int xPos = rEvtMouse.getXPos() - pos->getLeft();
456 VarTree::Iterator it;
458 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
461 VarTree::Iterator itClicked = findItemAtPos( yPos );
462 // Flag to know if the current item must be selected
464 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
466 it = m_flat ? m_rTree.getNextLeaf( it )
467 : m_rTree.getNextVisibleItem( it ) )
469 bool nextSelect = select;
470 if( it == itClicked || &*it == m_pLastSelected )
482 it->m_selected = (*it).m_selected || select;
486 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
489 // Invert the selection of the item
490 it = findItemAtPos( yPos );
491 if( it != m_rTree.end() )
493 it->m_selected = !it->m_selected;
494 m_pLastSelected = &*it;
497 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
500 VarTree::Iterator itClicked = findItemAtPos( yPos );
501 // Flag to know if the current item must be selected
503 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
505 it = m_flat ? m_rTree.getNextLeaf( it )
506 : m_rTree.getNextVisibleItem( it ) )
508 bool nextSelect = select;
509 if( it == itClicked || &*it == m_pLastSelected )
521 it->m_selected = select;
525 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
528 it = findItemAtPos(yPos);
529 if( it != m_rTree.end() )
531 if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
532 && xPos < it->depth() * itemImageWidth() )
535 // Fold/unfold the item
536 it->m_expanded = !it->m_expanded;
537 bChangedPosition = true;
541 // Unselect any previously selected item
542 VarTree::Iterator it2;
543 for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
544 it2 != m_rTree.end();
545 it2 = m_flat ? m_rTree.getNextLeaf( it2 )
546 : m_rTree.getNextVisibleItem( it2 ) )
548 it2->m_selected = false;
550 // Select the new item
551 if( it != m_rTree.end() )
553 it->m_selected = true;
554 m_pLastSelected = &*it;
560 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
563 it = findItemAtPos(yPos);
564 if( it != m_rTree.end() )
566 // Execute the action associated to this item
567 m_rTree.action( &*it );
570 // Redraw the control
575 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
577 int direction = ((EvtScroll&)rEvent).getDirection();
579 double percentage = m_rTree.getPositionVar().get();
580 double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
581 : m_rTree.visibleItems() );
582 if( direction == EvtScroll::kUp )
590 m_rTree.getPositionVar().set( percentage );
593 /* We changed the nodes, let's fix teh position var */
594 if( bChangedPosition )
596 VarTree::Iterator it;
599 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
601 it = m_flat ? m_rTree.getNextLeaf( it )
602 : m_rTree.getNextVisibleItem( it ) )
605 if( it == m_firstPos )
611 iFirst += maxItems();
612 if( iFirst >= m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems() )
613 iFirst = m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems();
614 float f_new = (float)iFirst / (float)( m_flat ? m_rTree.countLeafs()
615 :m_rTree.visibleItems() );
617 m_rTree.getPositionVar().set( 1.0 - f_new );
622 bool CtrlTree::mouseOver( int x, int y ) const
624 const Position *pPos = getPosition();
626 ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
630 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
634 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
638 bool CtrlTree::ensureVisible( VarTree::Iterator item )
640 // Find the item to focus
641 int focusItemIndex = 0;
642 VarTree::Iterator it;
644 m_rTree.ensureExpanded( item );
646 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
648 it = m_flat ? m_rTree.getNextLeaf( it )
649 : m_rTree.getNextVisibleItem( it ) )
651 if( it->m_id == item->m_id ) break;
654 return ensureVisible( focusItemIndex );
657 bool CtrlTree::ensureVisible( int focusItemIndex )
660 VarTree::Iterator it;
661 int firstPosIndex = 0;
662 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
664 it = m_flat ? m_rTree.getNextLeaf( it )
665 : m_rTree.getNextVisibleItem( it ) )
667 if( it == m_firstPos ) break;
671 if( it == m_rTree.end() ) return false;
674 if( it != m_rTree.end()
675 && ( focusItemIndex < firstPosIndex
676 || focusItemIndex > firstPosIndex + maxItems() ) )
678 // Scroll to have the wanted stream visible
679 VarPercent &rVarPos = m_rTree.getPositionVar();
680 rVarPos.set( 1.0 - (double)focusItemIndex /
681 (double)( m_flat ? m_rTree.countLeafs()
682 : m_rTree.visibleItems() ) );
688 void CtrlTree::autoScroll()
690 // Find the current playing stream
692 VarTree::Iterator it;
694 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
696 it = m_flat ? m_rTree.getNextLeaf( it )
697 : m_rTree.getNextItem( it ) )
701 m_rTree.ensureExpanded( it );
705 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
707 it = m_flat ? m_rTree.getNextLeaf( it )
708 : m_rTree.getNextVisibleItem( it ) )
715 if( it == m_rTree.end() ) return;
718 ensureVisible( playIndex );
722 void CtrlTree::makeImage()
724 stats_TimerStart( getIntf(), "[Skins] Playlist image",
725 STATS_TIMER_SKINS_PLAYTREE_IMAGE );
728 // Get the size of the control
729 const Position *pPos = getPosition();
732 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
735 int width = pPos->getWidth();
736 int height = pPos->getHeight();
738 int i_itemHeight = itemHeight();
741 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
742 m_pImage = pOsFactory->createOSGraphics( width, height );
744 VarTree::Iterator it = m_firstPos;
748 // Draw the background bitmap
749 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
750 m_pImage->drawBitmap( bmp, 0, 0 );
752 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
754 if( it != m_rTree.end() )
756 if( (*it).m_selected )
758 int rectHeight = __MIN( i_itemHeight, height - yPos );
759 m_pImage->fillRect( 0, yPos, width, rectHeight,
764 it = m_flat ? m_rTree.getNextLeaf( it )
765 : m_rTree.getNextVisibleItem( it );
766 } while( it != m_rTree.end() && it->m_deleted );
773 // Fill background with background color
774 uint32_t bgColor = m_bgColor1;
775 m_pImage->fillRect( 0, 0, width, height, bgColor );
776 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
778 int rectHeight = __MIN( i_itemHeight, height - yPos );
779 if( it != m_rTree.end() )
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 );
791 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
793 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
797 int bitmapWidth = itemImageWidth();
801 while( it != m_rTree.end() && yPos < height )
803 const GenericBitmap *m_pCurBitmap;
804 UString *pStr = (UString*)(it->m_cString.get());
805 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
810 int depth = m_flat ? 1 : it->depth();
811 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
814 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
818 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
820 m_pCurBitmap = m_pItemBitmap;
824 // Make sure we are centered on the line
825 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
826 if( yPos2 >= height )
831 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
832 bitmapWidth * (depth - 1 ), yPos2,
833 m_pCurBitmap->getWidth(),
834 __MIN( m_pCurBitmap->getHeight(),
835 height - yPos2), true );
837 yPos += i_itemHeight - pText->getHeight();
844 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
845 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
848 yPos += (pText->getHeight() - ySrc );
853 it = m_flat ? m_rTree.getNextLeaf( it )
854 : m_rTree.getNextVisibleItem( it );
855 } while( it != m_rTree.end() && it->m_deleted );
857 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
860 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
862 // The first item is m_firstPos.
863 // We decrement pos as we try the other items, until pos == 0.
864 VarTree::Iterator it;
865 for( it = m_firstPos; it != m_rTree.end() && pos != 0;
866 it = m_flat ? m_rTree.getNextLeaf( it )
867 : m_rTree.getNextVisibleItem( it ) )