1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #include "ctrl_tree.hpp"
26 #include "../src/os_factory.hpp"
27 #include "../src/os_graphics.hpp"
28 #include "../src/generic_bitmap.hpp"
29 #include "../src/generic_font.hpp"
30 #include "../src/scaled_bitmap.hpp"
31 #include "../utils/position.hpp"
32 #include "../utils/ustring.hpp"
33 #include "../events/evt_key.hpp"
34 #include "../events/evt_mouse.hpp"
35 #include "../events/evt_scroll.hpp"
38 # include "solaris_specific.h" // for lrint
41 #define SCROLL_STEP 0.05
42 #define LINE_INTERVAL 1 // Number of pixels inserted between 2 lines
45 CtrlTree::CtrlTree( intf_thread_t *pIntf,
47 const GenericFont &rFont,
48 const GenericBitmap *pBgBitmap,
49 const GenericBitmap *pItemBitmap,
50 const GenericBitmap *pOpenBitmap,
51 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_firstPos = m_rTree.begin();
77 m_rTree.getPositionVar().delObserver( this );
78 m_rTree.delObserver( this );
85 int CtrlTree::itemHeight()
87 int itemHeight = m_rFont.getSize();
90 itemHeight = __MAX( m_pClosedBitmap->getHeight(), itemHeight );
94 itemHeight = __MAX( m_pOpenBitmap->getHeight(), itemHeight );
98 itemHeight = __MAX( m_pItemBitmap->getHeight(), itemHeight );
100 itemHeight += LINE_INTERVAL;
104 int CtrlTree::itemImageWidth()
107 if( m_pClosedBitmap )
109 bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
113 bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
117 bitmapWidth = __MAX( m_pItemBitmap->getWidth(), bitmapWidth );
119 return bitmapWidth + 2;
122 int CtrlTree::maxItems()
124 const Position *pPos = getPosition();
129 return pPos->getHeight() / itemHeight();
133 void CtrlTree::onUpdate( Subject<VarTree, tree_update*> &rTree,
136 if( arg->i_type == 0 ) // Item update
138 if( arg->b_active_item )
141 ///\todo We should make image if we are visible in the view
145 /// \todo handle delete in a more clever way
146 else if ( arg->i_type == 1 ) // Global change or deletion
148 m_firstPos = m_rTree.begin();
151 else if ( arg->i_type == 2 ) // Item-append
153 /// \todo Check if the item is really visible in the view
154 // (we only check if it in the document)
155 if( arg->b_visible == true )
160 else if( arg->i_type == 3 ) // item-del
162 /* Make sure firstPos and lastSelected are still valid */
163 while( m_firstPos->m_deleted && m_firstPos != m_rTree.root()->begin() )
165 m_firstPos = m_rTree.getPrevVisibleItem( m_firstPos );
167 if( m_firstPos->m_deleted ) m_firstPos = m_rTree.root()->begin();
169 if( arg->b_visible == true )
177 void CtrlTree::onUpdate( Subject<VarPercent, void*> &rPercent, void* arg)
179 // Determine what is the first item to display
180 VarTree::Iterator it = m_rTree.begin();
182 if( m_dontMove ) return;
184 int excessItems = m_rTree.visibleItems() - maxItems();
188 VarPercent &rVarPos = m_rTree.getPositionVar();
189 // a simple (int)(...) causes rounding errors !
193 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
195 if( m_firstPos != it )
197 // Redraw the control if the position has changed
204 void CtrlTree::onResize()
206 // Determine what is the first item to display
207 VarTree::Iterator it = m_rTree.begin();
209 int excessItems = m_rTree.visibleItems() - maxItems();
213 VarPercent &rVarPos = m_rTree.getPositionVar();
214 // a simple (int)(...) causes rounding errors !
218 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
220 // Redraw the control if the position has changed
226 void CtrlTree::onPositionChange()
232 void CtrlTree::handleEvent( EvtGeneric &rEvent )
234 bool bChangedPosition = false;
235 VarTree::Iterator toShow; bool needShow = false;
236 if( rEvent.getAsString().find( "key:down" ) != string::npos )
238 int key = ((EvtKey&)rEvent).getKey();
239 VarTree::Iterator it;
240 bool previousWasSelected = false;
242 /* Delete the selection */
243 if( key == KEY_DELETE )
245 m_rTree.delSelected();
247 else if( key == KEY_PAGEDOWN )
250 int i = (int)(maxItems()*1.5);
253 VarTree::Iterator it_old = it;
254 it = m_rTree.getNextVisibleItem( it );
255 /* End is already visible, dont' scroll */
256 if( it == m_rTree.end() )
272 else if (key == KEY_PAGEUP )
276 while( i >= maxItems()/2 )
278 it = m_rTree.getPrevVisibleItem( it );
279 /* End is already visible, dont' scroll */
280 if( it == m_rTree.begin() )
293 for( it = m_rTree.begin(); it != m_rTree.end();
294 it = m_rTree.getNextVisibleItem( it ) )
296 VarTree::Iterator next = m_rTree.getNextVisibleItem( it );
299 // Scroll up one item
301 && it != it->parent()->begin() )
302 || &*it != m_pLastSelected )
304 bool nextWasSelected = ( &*next == m_pLastSelected );
305 it->m_selected = nextWasSelected;
306 if( nextWasSelected )
308 m_pLastSelected = &*it;
309 needShow = true; toShow = it;
313 else if( key == KEY_DOWN )
315 // Scroll down one item
317 && next != it->parent()->end() )
318 || &*it != m_pLastSelected )
320 (*it).m_selected = previousWasSelected;
322 if( previousWasSelected )
324 m_pLastSelected = &*it;
325 needShow = true; toShow = it;
326 previousWasSelected = false;
330 previousWasSelected = ( &*it == m_pLastSelected );
333 else if( key == KEY_RIGHT )
335 // Go down one level (and expand node)
336 if( &*it == m_pLastSelected )
342 it->m_selected = false;
343 it->begin()->m_selected = true;
344 m_pLastSelected = &*(it->begin());
348 m_rTree.action( &*it );
353 it->m_expanded = true;
354 bChangedPosition = true;
358 else if( key == KEY_LEFT )
360 // Go up one level (and close node)
361 if( &*it == m_pLastSelected )
363 if( it->m_expanded && it->size() )
365 it->m_expanded = false;
366 bChangedPosition = true;
370 if( it->parent() && it->parent() != &m_rTree)
372 it->m_selected = false;
373 m_pLastSelected = it->parent();
374 m_pLastSelected->m_selected = true;
379 else if( key == KEY_ENTER || key == KEY_SPACE )
381 // Go up one level (and close node)
382 if( &*it == m_pLastSelected )
384 m_rTree.action( &*it );
389 ensureVisible( toShow );
391 // Redraw the control
396 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
398 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
399 const Position *pos = getPosition();
400 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
401 int xPos = rEvtMouse.getXPos() - pos->getLeft();
402 VarTree::Iterator it;
404 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
407 VarTree::Iterator itClicked = findItemAtPos( yPos );
408 // Flag to know if the current item must be selected
410 for( it = m_rTree.begin(); it != m_rTree.end();
411 it = m_rTree.getNextVisibleItem( it ) )
413 bool nextSelect = select;
414 if( it == itClicked || &*it == m_pLastSelected )
426 it->m_selected = (*it).m_selected || select;
430 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
433 // Invert the selection of the item
434 it = findItemAtPos( yPos );
435 if( it != m_rTree.end() )
437 it->m_selected = !it->m_selected;
438 m_pLastSelected = &*it;
441 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
444 VarTree::Iterator itClicked = findItemAtPos( yPos );
445 // Flag to know if the current item must be selected
447 for( it = m_rTree.begin(); it != m_rTree.end();
448 it = m_rTree.getNextVisibleItem( it ) )
450 bool nextSelect = select;
451 if( it == itClicked || &*it == m_pLastSelected )
463 it->m_selected = select;
467 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
470 it = findItemAtPos(yPos);
471 if( it != m_rTree.end() )
473 if( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
474 && xPos < it->depth() * itemImageWidth() )
476 // Fold/unfold the item
477 it->m_expanded = !it->m_expanded;
478 bChangedPosition = true;
482 // Unselect any previously selected item
483 VarTree::Iterator it2;
484 for( it2 = m_rTree.begin(); it2 != m_rTree.end();
485 it2 = m_rTree.getNextVisibleItem( it2 ) )
487 it2->m_selected = false;
489 // Select the new item
490 if( it != m_rTree.end() )
492 it->m_selected = true;
493 m_pLastSelected = &*it;
499 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
502 it = findItemAtPos(yPos);
503 if( it != m_rTree.end() )
505 // Execute the action associated to this item
506 m_rTree.action( &*it );
509 // Redraw the control
514 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
516 int direction = ((EvtScroll&)rEvent).getDirection();
518 double percentage = m_rTree.getPositionVar().get();
519 double step = 2.0 / (double)m_rTree.visibleItems();
520 if( direction == EvtScroll::kUp )
528 m_rTree.getPositionVar().set( percentage );
531 /* We changed the nodes, let's fix teh position var */
532 if( bChangedPosition )
534 VarTree::Iterator it;
537 for( it = m_rTree.begin(); it != m_rTree.end();
538 it = m_rTree.getNextVisibleItem( it ) )
541 if( it == m_firstPos )
547 iFirst += maxItems();
548 if( iFirst >= m_rTree.visibleItems() ) iFirst = m_rTree.visibleItems();
549 float f_new = (float)iFirst / (float)m_rTree.visibleItems();
551 m_rTree.getPositionVar().set( 1.0 - f_new );
556 bool CtrlTree::mouseOver( int x, int y ) const
558 const Position *pPos = getPosition();
560 ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
564 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
568 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
572 bool CtrlTree::ensureVisible( VarTree::Iterator item )
574 // Find the item to focus
575 int focusItemIndex = 0;
576 VarTree::Iterator it;
578 m_rTree.ensureExpanded( item );
580 for( it = m_rTree.begin(); it != m_rTree.end();
581 it = m_rTree.getNextVisibleItem( it ) )
583 if( it->m_id == item->m_id ) break;
586 return ensureVisible( focusItemIndex );
589 bool CtrlTree::ensureVisible( int focusItemIndex )
592 VarTree::Iterator it;
593 int firstPosIndex = 0;
594 for( it = m_rTree.begin(); it != m_rTree.end();
595 it = m_rTree.getNextVisibleItem( it ) )
597 if( it == m_firstPos ) break;
601 if( it == m_rTree.end() ) return false;
604 if( it != m_rTree.end()
605 && ( focusItemIndex < firstPosIndex
606 || focusItemIndex > firstPosIndex + maxItems() ) )
608 // Scroll to have the wanted stream visible
609 VarPercent &rVarPos = m_rTree.getPositionVar();
610 rVarPos.set( 1.0 - (double)focusItemIndex /
611 (double)m_rTree.visibleItems() );
617 void CtrlTree::autoScroll()
619 // Find the current playing stream
621 VarTree::Iterator it;
623 for( it = m_rTree.begin(); it != m_rTree.end();
624 it = m_rTree.getNextItem( it ) )
628 m_rTree.ensureExpanded( it );
632 for( it = m_rTree.begin(); it != m_rTree.end();
633 it = m_rTree.getNextVisibleItem( it ) )
640 if( it == m_rTree.end() ) return;
643 ensureVisible( playIndex );
647 void CtrlTree::makeImage()
649 stats_TimerStart( getIntf(), "[Skins] Playlist image",
650 STATS_TIMER_SKINS_PLAYTREE_IMAGE );
656 // Get the size of the control
657 const Position *pPos = getPosition();
660 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
663 int width = pPos->getWidth();
664 int height = pPos->getHeight();
666 int i_itemHeight = itemHeight();
669 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
670 m_pImage = pOsFactory->createOSGraphics( width, height );
672 VarTree::Iterator it = m_firstPos;
676 // Draw the background bitmap
677 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
678 m_pImage->drawBitmap( bmp, 0, 0 );
680 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
682 if( it != m_rTree.end() )
684 if( (*it).m_selected )
686 int rectHeight = __MIN( i_itemHeight, height - yPos );
687 m_pImage->fillRect( 0, yPos, width, rectHeight,
692 it = m_rTree.getNextVisibleItem( it );
693 } while( it->m_deleted );
700 // Fill background with background color
701 uint32_t bgColor = m_bgColor1;
702 m_pImage->fillRect( 0, 0, width, height, bgColor );
703 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
705 int rectHeight = __MIN( i_itemHeight, height - yPos );
706 if( it != m_rTree.end() )
708 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
709 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
712 it = m_rTree.getNextVisibleItem( it );
713 } while( it->m_deleted );
717 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
719 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
723 int bitmapWidth = itemImageWidth();
727 while( it != m_rTree.end() && yPos < height )
729 const GenericBitmap *m_pCurBitmap;
730 UString *pStr = (UString*)(it->m_cString.get());
731 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
736 int depth = it->depth();
737 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
740 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
744 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
746 m_pCurBitmap = m_pItemBitmap;
750 // Make sure we are centered on the line
751 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
752 if( yPos2 >= height )
757 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
758 bitmapWidth * (depth - 1 ), yPos2,
759 m_pCurBitmap->getWidth(),
760 __MIN( m_pCurBitmap->getHeight(),
761 height - yPos2), true );
763 yPos += i_itemHeight - pText->getHeight();
770 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
771 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
774 yPos += (pText->getHeight() - ySrc );
778 it = m_rTree.getNextVisibleItem( it );
779 } while( it->m_deleted );
781 stats_TimerStop( getIntf(), STATS_TIMER_SKINS_PLAYTREE_IMAGE );
784 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
786 // The first item is m_firstPos.
787 // We decrement pos as we try the other items, until pos == 0.
788 VarTree::Iterator it;
789 for( it = m_firstPos; it != m_rTree.end() && pos != 0;
790 it = m_rTree.getNextVisibleItem( it ) )