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 m_firstPos = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
143 if( arg->i_type == 0 ) // Item update
145 if( arg->b_active_item )
148 ///\todo We should make image if we are visible in the view
152 /// \todo handle delete in a more clever way
153 else if ( arg->i_type == 1 ) // Global change or deletion
157 else if ( arg->i_type == 2 ) // Item-append
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 )
168 else if( arg->i_type == 3 ) // item-del
170 /* Make sure firstPos and lastSelected are still valid */
171 while( m_firstPos->m_deleted && m_firstPos != m_rTree.root()->begin() )
173 m_firstPos = m_flat ? m_rTree.getPrevLeaf( m_firstPos )
174 : m_rTree.getPrevVisibleItem( m_firstPos );
176 if( m_firstPos->m_deleted )
177 m_firstPos = m_flat ? m_rTree.firstLeaf()
178 : m_rTree.root()->begin();
180 if( arg->b_visible == true )
188 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent, void* arg)
190 // Determine what is the first item to display
191 VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
193 if( m_dontMove ) return;
197 excessItems = m_rTree.countLeafs() - maxItems();
199 excessItems = m_rTree.visibleItems() - maxItems();
203 VarPercent &rVarPos = m_rTree.getPositionVar();
204 // a simple (int)(...) causes rounding errors !
209 it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
211 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
213 if( m_firstPos != it )
215 // Redraw the control if the position has changed
222 void CtrlTree::onResize()
224 // Determine what is the first item to display
225 VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
229 excessItems = m_rTree.countLeafs() - maxItems();
231 excessItems = m_rTree.visibleItems() - maxItems();
235 VarPercent &rVarPos = m_rTree.getPositionVar();
236 // a simple (int)(...) causes rounding errors !
241 it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
243 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
245 // Redraw the control if the position has changed
250 void CtrlTree::onPositionChange()
255 void CtrlTree::handleEvent( EvtGeneric &rEvent )
257 bool bChangedPosition = false;
258 VarTree::Iterator toShow; bool needShow = false;
259 if( rEvent.getAsString().find( "key:down" ) != string::npos )
261 int key = ((EvtKey&)rEvent).getKey();
262 VarTree::Iterator it;
263 bool previousWasSelected = false;
265 /* Delete the selection */
266 if( key == KEY_DELETE )
268 /* Find first non selected item before m_pLastSelected */
269 VarTree::Iterator it_sel = m_flat ? m_rTree.firstLeaf()
271 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
273 it = m_flat ? m_rTree.getNextLeaf( it )
274 : m_rTree.getNextVisibleItem( it ) )
276 if( &*it == m_pLastSelected ) break;
277 if( !it->m_selected ) it_sel = it;
280 /* Delete selected stuff */
281 m_rTree.delSelected();
284 it_sel->m_selected = true;
285 m_pLastSelected = &*it_sel;
287 else if( key == KEY_PAGEDOWN )
290 int i = (int)(maxItems()*1.5);
293 VarTree::Iterator it_old = it;
294 it = m_flat ? m_rTree.getNextLeaf( it )
295 : m_rTree.getNextVisibleItem( it );
296 /* End is already visible, dont' scroll */
297 if( it == m_rTree.end() )
313 else if (key == KEY_PAGEUP )
317 while( i >= maxItems()/2 )
319 it = m_flat ? m_rTree.getPrevLeaf( it )
320 : m_rTree.getPrevVisibleItem( it );
321 /* End is already visible, dont' scroll */
322 if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
335 // other keys to be forwarded to vlc core
336 EvtKey& rEvtKey = (EvtKey&)rEvent;
337 var_SetInteger( getIntf()->p_libvlc, "key-pressed",
338 rEvtKey.getModKey() );
343 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
345 it = m_flat ? m_rTree.getNextLeaf( it )
346 : m_rTree.getNextVisibleItem( it ) )
348 VarTree::Iterator next = m_flat ? m_rTree.getNextLeaf( it )
349 : m_rTree.getNextVisibleItem( it );
352 // Scroll up one item
354 && it != it->parent()->begin() )
355 || &*it != m_pLastSelected )
357 bool nextWasSelected = ( &*next == m_pLastSelected );
358 it->m_selected = nextWasSelected;
359 if( nextWasSelected )
361 m_pLastSelected = &*it;
362 needShow = true; toShow = it;
366 else if( key == KEY_DOWN )
368 // Scroll down one item
370 && next != it->parent()->end() )
371 || &*it != m_pLastSelected )
373 (*it).m_selected = previousWasSelected;
375 if( previousWasSelected )
377 m_pLastSelected = &*it;
378 needShow = true; toShow = it;
379 previousWasSelected = false;
383 previousWasSelected = ( &*it == m_pLastSelected );
386 // Fix last tree item selection
387 if( ( m_flat ? m_rTree.getNextLeaf( it )
388 : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
389 && &*it == m_pLastSelected )
391 (*it).m_selected = true;
394 else if( key == KEY_RIGHT )
396 // Go down one level (and expand node)
397 if( &*it == m_pLastSelected )
403 it->m_selected = false;
404 it->begin()->m_selected = true;
405 m_pLastSelected = &*(it->begin());
409 m_rTree.action( &*it );
414 it->m_expanded = true;
415 bChangedPosition = true;
419 else if( key == KEY_LEFT )
421 // Go up one level (and close node)
422 if( &*it == m_pLastSelected )
424 if( it->m_expanded && it->size() )
426 it->m_expanded = false;
427 bChangedPosition = true;
431 if( it->parent() && it->parent() != &m_rTree)
433 it->m_selected = false;
434 m_pLastSelected = it->parent();
435 m_pLastSelected->m_selected = true;
440 else if( key == KEY_ENTER || key == ' ' )
442 // Go up one level (and close node)
443 if( &*it == m_pLastSelected )
445 m_rTree.action( &*it );
450 ensureVisible( toShow );
452 // Redraw the control
457 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
459 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
460 const Position *pos = getPosition();
461 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
462 int xPos = rEvtMouse.getXPos() - pos->getLeft();
463 VarTree::Iterator it;
465 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
468 VarTree::Iterator itClicked = findItemAtPos( yPos );
469 // Flag to know if the current item must be selected
471 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
473 it = m_flat ? m_rTree.getNextLeaf( it )
474 : m_rTree.getNextVisibleItem( it ) )
476 bool nextSelect = select;
477 if( it == itClicked || &*it == m_pLastSelected )
489 it->m_selected = (*it).m_selected || select;
493 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
496 // Invert the selection of the item
497 it = findItemAtPos( yPos );
498 if( it != m_rTree.end() )
500 it->m_selected = !it->m_selected;
501 m_pLastSelected = &*it;
504 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
507 VarTree::Iterator itClicked = findItemAtPos( yPos );
508 // Flag to know if the current item must be selected
510 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
512 it = m_flat ? m_rTree.getNextLeaf( it )
513 : m_rTree.getNextVisibleItem( it ) )
515 bool nextSelect = select;
516 if( it == itClicked || &*it == m_pLastSelected )
528 it->m_selected = select;
532 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
535 it = findItemAtPos(yPos);
536 if( it != m_rTree.end() )
538 if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
539 && xPos < it->depth() * itemImageWidth() )
542 // Fold/unfold the item
543 it->m_expanded = !it->m_expanded;
544 bChangedPosition = true;
548 // Unselect any previously selected item
549 VarTree::Iterator it2;
550 for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
551 it2 != m_rTree.end();
552 it2 = m_flat ? m_rTree.getNextLeaf( it2 )
553 : m_rTree.getNextVisibleItem( it2 ) )
555 it2->m_selected = false;
557 // Select the new item
558 if( it != m_rTree.end() )
560 it->m_selected = true;
561 m_pLastSelected = &*it;
567 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
570 it = findItemAtPos(yPos);
571 if( it != m_rTree.end() )
573 // Execute the action associated to this item
574 m_rTree.action( &*it );
577 // Redraw the control
582 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
584 // XXX ctrl_slider.cpp has two more (but slightly different)
585 // XXX implementations of `scroll'. Figure out where it belongs.
587 int direction = static_cast<EvtScroll&>(rEvent).getDirection();
589 double percentage = m_rTree.getPositionVar().get();
590 double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
591 : m_rTree.visibleItems() );
592 if( direction == EvtScroll::kUp )
600 m_rTree.getPositionVar().set( percentage );
603 /* We changed the nodes, let's fix teh position var */
604 if( bChangedPosition )
606 VarTree::Iterator it;
609 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
611 it = m_flat ? m_rTree.getNextLeaf( it )
612 : m_rTree.getNextVisibleItem( it ) )
615 if( it == m_firstPos )
621 iFirst += maxItems();
622 if( iFirst >= m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems() )
623 iFirst = m_flat ? m_rTree.countLeafs() : m_rTree.visibleItems();
624 float f_new = (float)iFirst / (float)( m_flat ? m_rTree.countLeafs()
625 :m_rTree.visibleItems() );
627 m_rTree.getPositionVar().set( 1.0 - f_new );
632 bool CtrlTree::mouseOver( int x, int y ) const
634 const Position *pPos = getPosition();
635 return !pPos ? false :
636 x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight();
639 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h)
641 const Position *pPos = getPosition();
642 rect region( pPos->getLeft(), pPos->getTop(),
643 pPos->getWidth(), pPos->getHeight() );
644 rect clip( xDest, yDest, w, h );
647 if( rect::intersect( region, clip, &inter ) && m_pImage )
648 rImage.drawGraphics( *m_pImage,
649 inter.x - pPos->getLeft(),
650 inter.y - pPos->getTop(),
651 inter.x, inter.y, inter.width, inter.height );
654 bool CtrlTree::ensureVisible( VarTree::Iterator item )
656 // Find the item to focus
657 int focusItemIndex = 0;
658 VarTree::Iterator it;
660 m_rTree.ensureExpanded( item );
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_id == item->m_id ) break;
670 return ensureVisible( focusItemIndex );
673 bool CtrlTree::ensureVisible( int focusItemIndex )
676 VarTree::Iterator it;
677 int firstPosIndex = 0;
678 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
680 it = m_flat ? m_rTree.getNextLeaf( it )
681 : m_rTree.getNextVisibleItem( it ) )
683 if( it == m_firstPos ) break;
687 if( it == m_rTree.end() ) return false;
690 if( it != m_rTree.end()
691 && ( focusItemIndex < firstPosIndex
692 || focusItemIndex > firstPosIndex + maxItems() ) )
694 // Scroll to have the wanted stream visible
695 VarPercent &rVarPos = m_rTree.getPositionVar();
696 rVarPos.set( 1.0 - (double)focusItemIndex /
697 (double)( m_flat ? m_rTree.countLeafs()
698 : m_rTree.visibleItems() ) );
704 void CtrlTree::autoScroll()
706 // Find the current playing stream
708 VarTree::Iterator it;
710 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
712 it = m_flat ? m_rTree.getNextLeaf( it )
713 : m_rTree.getNextItem( it ) )
717 m_rTree.ensureExpanded( it );
721 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
723 it = m_flat ? m_rTree.getNextLeaf( it )
724 : m_rTree.getNextVisibleItem( it ) )
731 if( it == m_rTree.end() ) return;
734 ensureVisible( playIndex );
738 void CtrlTree::makeImage()
740 stats_TimerStart( getIntf(), "[Skins] Playlist image",
741 STATS_TIMER_SKINS_PLAYTREE_IMAGE );
744 // Get the size of the control
745 const Position *pPos = getPosition();
748 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
751 int width = pPos->getWidth();
752 int height = pPos->getHeight();
754 int i_itemHeight = itemHeight();
757 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
758 m_pImage = pOsFactory->createOSGraphics( width, height );
760 VarTree::Iterator it = m_firstPos;
764 // Draw the background bitmap
765 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
766 m_pImage->drawBitmap( bmp, 0, 0 );
768 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
770 if( it != m_rTree.end() )
772 if( (*it).m_selected )
774 int rectHeight = __MIN( i_itemHeight, height - yPos );
775 m_pImage->fillRect( 0, yPos, width, rectHeight,
780 it = m_flat ? m_rTree.getNextLeaf( it )
781 : m_rTree.getNextVisibleItem( it );
782 } while( it != m_rTree.end() && it->m_deleted );
789 // Fill background with background color
790 uint32_t bgColor = m_bgColor1;
791 m_pImage->fillRect( 0, 0, width, height, bgColor );
792 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
794 int rectHeight = __MIN( i_itemHeight, height - yPos );
795 if( it == m_rTree.end() )
796 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
799 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
800 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
803 it = m_flat ? m_rTree.getNextLeaf( it )
804 : m_rTree.getNextVisibleItem( it );
805 } while( it != m_rTree.end() && it->m_deleted );
807 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
811 int bitmapWidth = itemImageWidth();
815 while( it != m_rTree.end() && yPos < height )
817 const GenericBitmap *m_pCurBitmap;
818 UString *pStr = (UString*)(it->m_cString.get());
819 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
824 int depth = m_flat ? 1 : it->depth();
825 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
828 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
832 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
834 m_pCurBitmap = m_pItemBitmap;
838 // Make sure we are centered on the line
839 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
840 if( yPos2 >= height )
845 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
846 bitmapWidth * (depth - 1 ), yPos2,
847 m_pCurBitmap->getWidth(),
848 __MIN( m_pCurBitmap->getHeight(),
849 height - yPos2), true );
851 yPos += i_itemHeight - pText->getHeight();
858 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
859 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
862 yPos += (pText->getHeight() - ySrc );
867 it = m_flat ? m_rTree.getNextLeaf( it )
868 : m_rTree.getNextVisibleItem( it );
869 } while( it != m_rTree.end() && it->m_deleted );
871 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
874 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
876 // The first item is m_firstPos.
877 // We decrement pos as we try the other items, until pos == 0.
878 VarTree::Iterator it;
879 for( it = m_firstPos; it != m_rTree.end() && pos != 0;
880 it = m_flat ? m_rTree.getNextLeaf( it )
881 : m_rTree.getNextVisibleItem( it ) )