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 and lastSelected are still 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();
285 it_sel->m_selected = true;
286 m_pLastSelected = &*it_sel;
288 // Redraw the control
292 else if( key == KEY_PAGEDOWN )
295 int i = (int)(maxItems()*1.5);
298 VarTree::Iterator it_old = it;
299 it = m_flat ? m_rTree.getNextLeaf( it )
300 : m_rTree.getNextVisibleItem( it );
301 /* End is already visible, dont' scroll */
302 if( it == m_rTree.end() )
317 else if (key == KEY_PAGEUP )
321 while( i >= maxItems()/2 )
323 it = m_flat ? m_rTree.getPrevLeaf( it )
324 : m_rTree.getPrevVisibleItem( it );
325 /* End is already visible, dont' scroll */
326 if( it == ( m_flat ? m_rTree.firstLeaf() : m_rTree.begin() ) )
336 else if ( key == KEY_UP ||
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 ?
349 m_rTree.getNextLeaf( it ) :
350 m_rTree.getNextVisibleItem( it );
353 // Scroll up one item
355 && it != it->parent()->begin() )
356 || &*it != m_pLastSelected )
358 bool nextWasSelected = ( &*next == m_pLastSelected );
359 it->m_selected = nextWasSelected;
360 if( nextWasSelected )
362 m_pLastSelected = &*it;
363 needShow = true; toShow = it;
367 else if( key == KEY_DOWN )
369 // Scroll down one item
371 && next != it->parent()->end() )
372 || &*it != m_pLastSelected )
374 (*it).m_selected = previousWasSelected;
376 if( previousWasSelected )
378 m_pLastSelected = &*it;
379 needShow = true; toShow = it;
380 previousWasSelected = false;
384 previousWasSelected = ( &*it == m_pLastSelected );
387 // Fix last tree item selection
388 if( ( m_flat ? m_rTree.getNextLeaf( it )
389 : m_rTree.getNextVisibleItem( it ) ) == m_rTree.end()
390 && &*it == m_pLastSelected )
392 (*it).m_selected = true;
395 else if( key == KEY_RIGHT )
397 // Go down one level (and expand node)
398 if( &*it == m_pLastSelected )
404 it->m_selected = false;
405 it->begin()->m_selected = true;
406 m_pLastSelected = &*(it->begin());
410 m_rTree.action( &*it );
415 it->m_expanded = true;
416 bChangedPosition = true;
420 else if( key == KEY_LEFT )
422 // Go up one level (and close node)
423 if( &*it == m_pLastSelected )
425 if( it->m_expanded && it->size() )
427 it->m_expanded = false;
428 bChangedPosition = true;
432 if( it->parent() && it->parent() != &m_rTree)
434 it->m_selected = false;
435 m_pLastSelected = it->parent();
436 m_pLastSelected->m_selected = true;
441 else if( key == KEY_ENTER || key == ' ' )
443 // Go up one level (and close node)
444 if( &*it == m_pLastSelected )
446 m_rTree.action( &*it );
451 ensureVisible( toShow );
452 // Redraw the control
458 // other keys to be forwarded to vlc core
459 EvtKey& rEvtKey = (EvtKey&)rEvent;
460 var_SetInteger( getIntf()->p_libvlc, "key-pressed",
461 rEvtKey.getModKey() );
466 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
468 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
469 const Position *pos = getPosition();
470 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
471 int xPos = rEvtMouse.getXPos() - pos->getLeft();
472 VarTree::Iterator it;
474 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
477 VarTree::Iterator itClicked = findItemAtPos( yPos );
478 // Flag to know if the current item must be selected
480 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
482 it = m_flat ? m_rTree.getNextLeaf( it )
483 : m_rTree.getNextVisibleItem( it ) )
485 bool nextSelect = select;
486 if( it == itClicked || &*it == m_pLastSelected )
498 it->m_selected = (*it).m_selected || select;
502 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
505 // Invert the selection of the item
506 it = findItemAtPos( yPos );
507 if( it != m_rTree.end() )
509 it->m_selected = !it->m_selected;
510 m_pLastSelected = &*it;
513 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
516 VarTree::Iterator itClicked = findItemAtPos( yPos );
517 // Flag to know if the current item must be selected
519 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
521 it = m_flat ? m_rTree.getNextLeaf( it )
522 : m_rTree.getNextVisibleItem( it ) )
524 bool nextSelect = select;
525 if( it == itClicked || &*it == m_pLastSelected )
537 it->m_selected = select;
541 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
544 it = findItemAtPos(yPos);
545 if( it != m_rTree.end() )
547 if( ( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
548 && xPos < it->depth() * itemImageWidth() )
551 // Fold/unfold the item
552 it->m_expanded = !it->m_expanded;
553 bChangedPosition = true;
557 // Unselect any previously selected item
558 VarTree::Iterator it2;
559 for( it2 = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
560 it2 != m_rTree.end();
561 it2 = m_flat ? m_rTree.getNextLeaf( it2 )
562 : m_rTree.getNextVisibleItem( it2 ) )
564 it2->m_selected = false;
566 // Select the new item
567 if( it != m_rTree.end() )
569 it->m_selected = true;
570 m_pLastSelected = &*it;
576 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
579 it = findItemAtPos(yPos);
580 if( it != m_rTree.end() )
582 // Execute the action associated to this item
583 m_rTree.action( &*it );
586 // Redraw the control
591 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
593 // XXX ctrl_slider.cpp has two more (but slightly different)
594 // XXX implementations of `scroll'. Figure out where it belongs.
596 int direction = static_cast<EvtScroll&>(rEvent).getDirection();
598 double percentage = m_rTree.getPositionVar().get();
599 double step = 2.0 / (double)( m_flat ? m_rTree.countLeafs()
600 : m_rTree.visibleItems() );
601 if( direction == EvtScroll::kUp )
609 m_rTree.getPositionVar().set( percentage );
612 /* We changed the nodes, let's fix the position var */
613 if( bChangedPosition )
615 VarTree::Iterator it;
617 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
619 it = m_flat ? m_rTree.getNextLeaf( it )
620 : m_rTree.getNextVisibleItem( it ) )
622 if( it == m_firstPos )
627 int indexMax = ( m_flat ? m_rTree.countLeafs()
628 : m_rTree.visibleItems() ) - 1;
629 float f_new = (float)iFirst / (float)indexMax;
632 m_rTree.getPositionVar().set( 1.0 - f_new );
637 bool CtrlTree::mouseOver( int x, int y ) const
639 const Position *pPos = getPosition();
640 return !pPos ? false :
641 x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight();
644 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h)
646 const Position *pPos = getPosition();
647 rect region( pPos->getLeft(), pPos->getTop(),
648 pPos->getWidth(), pPos->getHeight() );
649 rect clip( xDest, yDest, w, h );
652 if( rect::intersect( region, clip, &inter ) && m_pImage )
653 rImage.drawGraphics( *m_pImage,
654 inter.x - pPos->getLeft(),
655 inter.y - pPos->getTop(),
656 inter.x, inter.y, inter.width, inter.height );
659 bool CtrlTree::ensureVisible( VarTree::Iterator item )
661 // Find the item to focus
662 int focusItemIndex = 0;
663 VarTree::Iterator it;
665 m_rTree.ensureExpanded( item );
667 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
669 it = m_flat ? m_rTree.getNextLeaf( it )
670 : m_rTree.getNextVisibleItem( it ) )
672 if( it->m_id == item->m_id ) break;
675 return ensureVisible( focusItemIndex );
678 bool CtrlTree::ensureVisible( int focusItemIndex )
681 VarTree::Iterator it;
682 int firstPosIndex = 0;
683 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
685 it = m_flat ? m_rTree.getNextLeaf( it )
686 : m_rTree.getNextVisibleItem( it ) )
688 if( it == m_firstPos ) break;
692 if( it == m_rTree.end() ) return false;
695 if( it != m_rTree.end()
696 && ( 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 ) )
722 m_rTree.ensureExpanded( it );
727 for( it = m_flat ? m_rTree.firstLeaf() : m_rTree.begin();
729 it = m_flat ? m_rTree.getNextLeaf( it )
730 : m_rTree.getNextVisibleItem( it ) )
734 ensureVisible( playIndex );
742 void CtrlTree::makeImage()
744 stats_TimerStart( getIntf(), "[Skins] Playlist image",
745 STATS_TIMER_SKINS_PLAYTREE_IMAGE );
748 // Get the size of the control
749 const Position *pPos = getPosition();
752 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
755 int width = pPos->getWidth();
756 int height = pPos->getHeight();
758 int i_itemHeight = itemHeight();
761 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
762 m_pImage = pOsFactory->createOSGraphics( width, height );
764 VarTree::Iterator it = m_firstPos;
768 // Draw the background bitmap
769 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
770 m_pImage->drawBitmap( bmp, 0, 0 );
772 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
774 if( it != m_rTree.end() )
776 if( (*it).m_selected )
778 int rectHeight = __MIN( i_itemHeight, height - yPos );
779 m_pImage->fillRect( 0, yPos, width, rectHeight,
784 it = m_flat ? m_rTree.getNextLeaf( it )
785 : m_rTree.getNextVisibleItem( it );
786 } while( it != m_rTree.end() && it->m_deleted );
793 // Fill background with background color
794 uint32_t bgColor = m_bgColor1;
795 m_pImage->fillRect( 0, 0, width, height, bgColor );
796 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
798 int rectHeight = __MIN( i_itemHeight, height - yPos );
799 if( it == m_rTree.end() )
800 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
803 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
804 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
807 it = m_flat ? m_rTree.getNextLeaf( it )
808 : m_rTree.getNextVisibleItem( it );
809 } while( it != m_rTree.end() && it->m_deleted );
811 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
815 int bitmapWidth = itemImageWidth();
819 while( it != m_rTree.end() && yPos < height )
821 const GenericBitmap *m_pCurBitmap;
822 UString *pStr = (UString*)(it->m_cString.get());
823 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
828 int depth = m_flat ? 1 : it->depth();
829 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
832 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
836 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
838 m_pCurBitmap = m_pItemBitmap;
842 // Make sure we are centered on the line
843 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
844 if( yPos2 >= height )
849 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
850 bitmapWidth * (depth - 1 ), yPos2,
851 m_pCurBitmap->getWidth(),
852 __MIN( m_pCurBitmap->getHeight(),
853 height - yPos2), true );
855 yPos += i_itemHeight - pText->getHeight();
862 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
863 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
866 yPos += (pText->getHeight() - ySrc );
871 it = m_flat ? m_rTree.getNextLeaf( it )
872 : m_rTree.getNextVisibleItem( it );
873 } while( it != m_rTree.end() && it->m_deleted );
875 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
878 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
880 // The first item is m_firstPos.
881 // We decrement pos as we try the other items, until pos == 0.
882 VarTree::Iterator it;
883 for( it = m_firstPos; it != m_rTree.end() && pos != 0;
884 it = m_flat ? m_rTree.getNextLeaf( it )
885 : m_rTree.getNextVisibleItem( it ) )