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();
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 is valid */
171 while( m_firstPos->m_deleted &&
172 m_firstPos != (m_flat ? m_rTree.firstLeaf()
175 m_firstPos = m_flat ? m_rTree.getPrevLeaf( m_firstPos )
176 : m_rTree.getPrevVisibleItem( m_firstPos );
178 if( m_firstPos->m_deleted )
179 m_firstPos = m_rTree.begin();
181 if( arg->b_visible == true )
189 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent, void* arg)
191 // Determine what is the first item to display
192 VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
194 if( m_dontMove ) return;
198 excessItems = m_rTree.countLeafs() - maxItems();
200 excessItems = m_rTree.visibleItems() - maxItems();
204 VarPercent &rVarPos = m_rTree.getPositionVar();
205 // a simple (int)(...) causes rounding errors !
210 it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
212 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
214 if( m_firstPos != it )
216 // Redraw the control if the position has changed
223 void CtrlTree::onResize()
225 // Determine what is the first item to display
226 VarTree::Iterator it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
230 excessItems = m_rTree.countLeafs() - maxItems();
232 excessItems = m_rTree.visibleItems() - maxItems();
236 VarPercent &rVarPos = m_rTree.getPositionVar();
237 // a simple (int)(...) causes rounding errors !
242 it = m_rTree.getLeaf(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
244 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1 );
246 // Redraw the control if the position has changed
251 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();
284 /* Verify if there is still sthg selected (e.g read-only items) */
285 m_pLastSelected = NULL;
286 for( it = (m_flat ? m_rTree.firstLeaf() : m_rTree.begin());
288 it = (m_flat ? m_rTree.getNextLeaf( it )
289 : m_rTree.getNextVisibleItem( it )) )
292 m_pLastSelected = &*it;
295 /* if everything was deleted, use it_sel as last selection */
296 if( !m_pLastSelected )
298 it_sel->m_selected = true;
299 m_pLastSelected = &*it_sel;
302 // Redraw the control
306 else if( key == KEY_PAGEDOWN )
309 int i = (int)(maxItems()*1.5);
312 VarTree::Iterator it_old = it;
313 it = m_flat ? m_rTree.getNextLeaf( it )
314 : m_rTree.getNextVisibleItem( it );
315 /* End is already visible, dont' scroll */
316 if( it == m_rTree.end() )
331 else if (key == KEY_PAGEUP )
335 while( i >= maxItems()/2 )
337 it = m_flat ? m_rTree.getPrevLeaf( it )
338 : m_rTree.getPrevVisibleItem( it );
339 /* End is already visible, dont' scroll */
340 if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
350 else if ( key == KEY_UP ||
357 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
359 it = m_flat ? m_rTree.getNextLeaf( it )
360 : m_rTree.getNextVisibleItem( it ) )
362 VarTree::Iterator next = m_flat ?
363 m_rTree.getNextLeaf( it ) :
364 m_rTree.getNextVisibleItem( it );
367 // Scroll up one item
369 && it != it->parent()->begin() )
370 || &*it != m_pLastSelected )
372 bool nextWasSelected = ( &*next == m_pLastSelected );
373 it->m_selected = nextWasSelected;
374 if( nextWasSelected )
376 m_pLastSelected = &*it;
377 needShow = true; toShow = it;
381 else if( key == KEY_DOWN )
383 // Scroll down one item
385 && next != it->parent()->end() )
386 || &*it != m_pLastSelected )
388 (*it).m_selected = previousWasSelected;
390 if( previousWasSelected )
392 m_pLastSelected = &*it;
393 needShow = true; toShow = it;
394 previousWasSelected = false;
398 previousWasSelected = ( &*it == m_pLastSelected );
401 // Fix last tree item selection
402 if( ( m_flat ? m_rTree.getNextLeaf( it )
403 : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
404 && &*it == m_pLastSelected )
406 (*it).m_selected = true;
409 else if( key == KEY_RIGHT )
411 // Go down one level (and expand node)
412 if( &*it == m_pLastSelected )
418 it->m_selected = false;
419 it->begin()->m_selected = true;
420 m_pLastSelected = &*(it->begin());
424 m_rTree.action( &*it );
429 it->m_expanded = true;
430 bChangedPosition = true;
434 else if( key == KEY_LEFT )
436 // Go up one level (and close node)
437 if( &*it == m_pLastSelected )
439 if( it->m_expanded && it->size() )
441 it->m_expanded = false;
442 bChangedPosition = true;
446 if( it->parent() && it->parent() != &m_rTree)
448 it->m_selected = false;
449 m_pLastSelected = it->parent();
450 m_pLastSelected->m_selected = true;
455 else if( key == KEY_ENTER || key == ' ' )
457 // Go up one level (and close node)
458 if( &*it == m_pLastSelected )
460 m_rTree.action( &*it );
465 ensureVisible( toShow );
466 // Redraw the control
472 // other keys to be forwarded to vlc core
473 EvtKey& rEvtKey = (EvtKey&)rEvent;
474 var_SetInteger( getIntf()->p_libvlc, "key-pressed",
475 rEvtKey.getModKey() );
480 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
482 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
483 const Position *pos = getPosition();
484 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
485 int xPos = rEvtMouse.getXPos() - pos->getLeft();
486 VarTree::Iterator it;
488 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
491 VarTree::Iterator itClicked = findItemAtPos( yPos );
492 // Flag to know if the current item must be selected
494 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
496 it = m_flat ? m_rTree.getNextLeaf( it )
497 : m_rTree.getNextVisibleItem( it ) )
499 bool nextSelect = select;
500 if( it == itClicked || &*it == m_pLastSelected )
512 it->m_selected = (*it).m_selected || select;
516 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
519 // Invert the selection of the item
520 it = findItemAtPos( yPos );
521 if( it != m_rTree.end() )
523 it->m_selected = !it->m_selected;
524 m_pLastSelected = &*it;
527 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
530 VarTree::Iterator itClicked = findItemAtPos( yPos );
531 // Flag to know if the current item must be selected
533 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
535 it = m_flat ? m_rTree.getNextLeaf( it )
536 : m_rTree.getNextVisibleItem( it ) )
538 bool nextSelect = select;
539 if( it == itClicked || &*it == m_pLastSelected )
551 it->m_selected = select;
555 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
558 it = findItemAtPos(yPos);
559 if( it != m_rTree.end() )
561 if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
562 && xPos < it->depth() * itemImageWidth() )
565 // Fold/unfold the item
566 it->m_expanded = !it->m_expanded;
567 bChangedPosition = true;
571 // Unselect any previously selected item
572 VarTree::Iterator it2;
573 for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
574 it2 != m_rTree.end();
575 it2 = m_flat ? m_rTree.getNextLeaf( it2 )
576 : m_rTree.getNextVisibleItem( it2 ) )
578 it2->m_selected = false;
580 // Select the new item
581 if( it != m_rTree.end() )
583 it->m_selected = true;
584 m_pLastSelected = &*it;
590 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
593 it = findItemAtPos(yPos);
594 if( it != m_rTree.end() )
596 // Execute the action associated to this item
597 m_rTree.action( &*it );
600 // Redraw the control
605 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
607 // XXX ctrl_slider.cpp has two more (but slightly different)
608 // XXX implementations of `scroll'. Figure out where it belongs.
610 int direction = static_cast<EvtScroll&>(rEvent).getDirection();
612 double percentage = m_rTree.getPositionVar().get();
613 double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
614 : m_rTree.visibleItems() );
615 if( direction == EvtScroll::kUp )
623 m_rTree.getPositionVar().set( percentage );
626 /* We changed the nodes, let's fix the position var */
627 if( bChangedPosition )
629 VarTree::Iterator it;
631 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
633 it = m_flat ? m_rTree.getNextLeaf( it )
634 : m_rTree.getNextVisibleItem( it ) )
636 if( it == m_firstPos )
641 int indexMax = ( m_flat ? m_rTree.countLeafs()
642 : m_rTree.visibleItems() ) - 1;
643 float f_new = (float)iFirst / (float)indexMax;
646 m_rTree.getPositionVar().set( 1.0 - f_new );
651 bool CtrlTree::mouseOver( int x, int y ) const
653 const Position *pPos = getPosition();
654 return !pPos ? false :
655 x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight();
658 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h)
660 const Position *pPos = getPosition();
661 rect region( pPos->getLeft(), pPos->getTop(),
662 pPos->getWidth(), pPos->getHeight() );
663 rect clip( xDest, yDest, w, h );
666 if( rect::intersect( region, clip, &inter ) && m_pImage )
667 rImage.drawGraphics( *m_pImage,
668 inter.x - pPos->getLeft(),
669 inter.y - pPos->getTop(),
670 inter.x, inter.y, inter.width, inter.height );
673 bool CtrlTree::ensureVisible( VarTree::Iterator item )
675 // Find the item to focus
676 int focusItemIndex = 0;
677 VarTree::Iterator it;
679 m_rTree.ensureExpanded( item );
681 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
683 it = m_flat ? m_rTree.getNextLeaf( it )
684 : m_rTree.getNextVisibleItem( it ) )
686 if( it->m_id == item->m_id ) break;
689 return ensureVisible( focusItemIndex );
692 bool CtrlTree::ensureVisible( int focusItemIndex )
695 VarTree::Iterator it;
696 int firstPosIndex = 0;
697 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
699 it = m_flat ? m_rTree.getNextLeaf( it )
700 : m_rTree.getNextVisibleItem( it ) )
702 if( it == m_firstPos ) break;
706 if( it == m_rTree.end() ) return false;
709 if( it != m_rTree.end()
710 && ( focusItemIndex < firstPosIndex
711 || focusItemIndex > firstPosIndex + maxItems() - 1 ) )
713 // Scroll to have the wanted stream visible
714 VarPercent &rVarPos = m_rTree.getPositionVar();
715 int indexMax = ( m_flat ? m_rTree.countLeafs()
716 : m_rTree.visibleItems() ) - 1;
717 rVarPos.set( 1.0 - (double)focusItemIndex / (double)indexMax );
723 void CtrlTree::autoScroll()
725 // Find the current playing stream
727 VarTree::Iterator it;
729 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
731 it = m_flat ? m_rTree.getNextLeaf( it )
732 : m_rTree.getNextItem( it ) )
736 m_rTree.ensureExpanded( it );
741 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
743 it = m_flat ? m_rTree.getNextLeaf( it )
744 : m_rTree.getNextVisibleItem( it ) )
748 ensureVisible( playIndex );
756 void CtrlTree::makeImage()
758 stats_TimerStart( getIntf(), "[Skins] Playlist image",
759 STATS_TIMER_SKINS_PLAYTREE_IMAGE );
762 // Get the size of the control
763 const Position *pPos = getPosition();
766 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
769 int width = pPos->getWidth();
770 int height = pPos->getHeight();
772 int i_itemHeight = itemHeight();
775 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
776 m_pImage = pOsFactory->createOSGraphics( width, height );
778 VarTree::Iterator it = m_firstPos;
782 // Draw the background bitmap
783 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
784 m_pImage->drawBitmap( bmp, 0, 0 );
786 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
788 if( it != m_rTree.end() )
790 if( (*it).m_selected )
792 int rectHeight = __MIN( i_itemHeight, height - yPos );
793 m_pImage->fillRect( 0, yPos, width, rectHeight,
798 it = m_flat ? m_rTree.getNextLeaf( it )
799 : m_rTree.getNextVisibleItem( it );
800 } while( it != m_rTree.end() && it->m_deleted );
807 // Fill background with background color
808 uint32_t bgColor = m_bgColor1;
809 m_pImage->fillRect( 0, 0, width, height, bgColor );
810 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
812 int rectHeight = __MIN( i_itemHeight, height - yPos );
813 if( it == m_rTree.end() )
814 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
817 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
818 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
821 it = m_flat ? m_rTree.getNextLeaf( it )
822 : m_rTree.getNextVisibleItem( it );
823 } while( it != m_rTree.end() && it->m_deleted );
825 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
829 int bitmapWidth = itemImageWidth();
833 while( it != m_rTree.end() && yPos < height )
835 const GenericBitmap *m_pCurBitmap;
836 UString *pStr = (UString*)(it->m_cString.get());
837 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
842 int depth = m_flat ? 1 : it->depth();
843 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
846 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
850 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
852 m_pCurBitmap = m_pItemBitmap;
856 // Make sure we are centered on the line
857 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
858 if( yPos2 >= height )
863 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
864 bitmapWidth * (depth - 1 ), yPos2,
865 m_pCurBitmap->getWidth(),
866 __MIN( m_pCurBitmap->getHeight(),
867 height - yPos2), true );
869 yPos += i_itemHeight - pText->getHeight();
876 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
877 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
880 yPos += (pText->getHeight() - ySrc );
885 it = m_flat ? m_rTree.getNextLeaf( it )
886 : m_rTree.getNextVisibleItem( it );
887 } while( it != m_rTree.end() && it->m_deleted );
889 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
892 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
894 // The first item is m_firstPos.
895 // We decrement pos as we try the other items, until pos == 0.
896 VarTree::Iterator it;
897 for( it = m_firstPos; it != m_rTree.end() && pos != 0;
898 it = m_flat ? m_rTree.getNextLeaf( it )
899 : m_rTree.getNextVisibleItem( it ) )