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 )
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
141 else if ( arg->i_type == 1 ) // Global change or deletion
143 m_firstPos = m_rTree.begin();
146 else if ( arg->i_type == 2 ) // Item-append
148 /// \todo Check if the really is really visible in the view (we only check if it in the document)
149 if( arg->b_visible == true )
155 m_pLastSelected = NULL;
158 void CtrlTree::onUpdate( Subject<VarPercent, void*> &rPercent, void* arg)
160 // Determine what is the first item to display
161 VarTree::Iterator it = m_rTree.begin();
163 int excessItems = m_rTree.visibleItems() - maxItems();
167 VarPercent &rVarPos = m_rTree.getPositionVar();
168 // a simple (int)(...) causes rounding errors !
172 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
174 if( m_firstPos != it )
176 // Redraw the control if the position has changed
183 void CtrlTree::onResize()
185 // Determine what is the first item to display
186 VarTree::Iterator it = m_rTree.begin();
188 int excessItems = m_rTree.visibleItems() - maxItems();
192 VarPercent &rVarPos = m_rTree.getPositionVar();
193 // a simple (int)(...) causes rounding errors !
197 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
199 // Redraw the control if the position has changed
205 void CtrlTree::onPositionChange()
211 void CtrlTree::handleEvent( EvtGeneric &rEvent )
213 VarTree::Iterator toShow; bool needShow = false;
214 if( rEvent.getAsString().find( "key:down" ) != string::npos )
216 int key = ((EvtKey&)rEvent).getKey();
217 VarTree::Iterator it;
218 bool previousWasSelected = false;
219 for( it = m_rTree.begin(); it != m_rTree.end();
220 it = m_rTree.getNextVisibleItem( it ) )
222 VarTree::Iterator next = m_rTree.getNextVisibleItem( it );
225 // Scroll up one item
227 && it != it->parent()->begin() )
228 || &*it != m_pLastSelected )
230 bool nextWasSelected = ( &*next == m_pLastSelected );
231 it->m_selected = nextWasSelected;
232 if( nextWasSelected )
234 m_pLastSelected = &*it;
235 needShow = true; toShow = it;
239 else if( key == KEY_DOWN )
241 // Scroll down one item
243 && next != it->parent()->end() )
244 || &*it != m_pLastSelected )
246 (*it).m_selected = previousWasSelected;
248 if( previousWasSelected )
250 m_pLastSelected = &*it;
251 needShow = true; toShow = it;
252 previousWasSelected = false;
256 previousWasSelected = ( &*it == m_pLastSelected );
259 else if( key == KEY_RIGHT )
261 // Go down one level (and expand node)
262 if( &*it == m_pLastSelected )
268 it->m_selected = false;
269 it->begin()->m_selected = true;
270 m_pLastSelected = &*(it->begin());
274 m_rTree.action( &*it );
279 it->m_expanded = true;
283 else if( key == KEY_LEFT )
285 // Go up one level (and close node)
286 if( &*it == m_pLastSelected )
288 if( it->m_expanded && it->size() )
290 it->m_expanded = false;
294 if( it->parent() && it->parent() != &m_rTree)
296 it->m_selected = false;
297 m_pLastSelected = it->parent();
298 m_pLastSelected->m_selected = true;
303 else if( key == KEY_ENTER || key == KEY_SPACE )
305 // Go up one level (and close node)
306 if( &*it == m_pLastSelected )
308 m_rTree.action( &*it );
313 ensureVisible( toShow );
315 // Redraw the control
320 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
322 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
323 const Position *pos = getPosition();
324 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
325 int xPos = rEvtMouse.getXPos() - pos->getLeft();
326 VarTree::Iterator it;
328 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
331 VarTree::Iterator itClicked = findItemAtPos( yPos );
332 // Flag to know if the current item must be selected
334 for( it = m_rTree.begin(); it != m_rTree.end();
335 it = m_rTree.getNextVisibleItem( it ) )
337 bool nextSelect = select;
338 if( it == itClicked || &*it == m_pLastSelected )
350 it->m_selected = (*it).m_selected || select;
354 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
357 // Invert the selection of the item
358 it = findItemAtPos( yPos );
359 if( it != m_rTree.end() )
361 it->m_selected = !it->m_selected;
362 m_pLastSelected = &*it;
365 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
368 VarTree::Iterator itClicked = findItemAtPos( yPos );
369 // Flag to know if the current item must be selected
371 for( it = m_rTree.begin(); it != m_rTree.end();
372 it = m_rTree.getNextVisibleItem( it ) )
374 bool nextSelect = select;
375 if( it == itClicked || &*it == m_pLastSelected )
387 it->m_selected = select;
391 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
394 it = findItemAtPos(yPos);
395 if( it->size() && xPos > (it->depth() - 1) * itemImageWidth()
396 && xPos < it->depth() * itemImageWidth() )
398 // Fold/unfold the item
399 it->m_expanded = !it->m_expanded;
403 // Unselect any previously selected item
404 VarTree::Iterator it2;
405 for( it2 = m_rTree.begin(); it2 != m_rTree.end();
406 it2 = m_rTree.getNextVisibleItem( it2 ) )
408 it2->m_selected = false;
410 // Select the new item
411 if( it != m_rTree.end() )
413 it->m_selected = true;
414 m_pLastSelected = &*it;
419 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
422 it = findItemAtPos(yPos);
423 if( it != m_rTree.end() )
425 // Execute the action associated to this item
426 m_rTree.action( &*it );
429 // Redraw the control
434 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
436 int direction = ((EvtScroll&)rEvent).getDirection();
438 double percentage = m_rTree.getPositionVar().get();
439 double step = 2.0 / (double)m_rTree.visibleItems();
440 if( direction == EvtScroll::kUp )
448 m_rTree.getPositionVar().set( percentage );
452 bool CtrlTree::mouseOver( int x, int y ) const
454 const Position *pPos = getPosition();
456 ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
460 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
464 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
468 bool CtrlTree::ensureVisible( VarTree::Iterator item )
470 // Find the item to focus
471 int focusItemIndex = 0;
472 VarTree::Iterator it;
474 m_rTree.ensureExpanded( item );
476 for( it = m_rTree.begin(); it != m_rTree.end();
477 it = m_rTree.getNextVisibleItem( it ) )
479 if( it->m_id == item->m_id ) break;
482 return ensureVisible( focusItemIndex );
485 bool CtrlTree::ensureVisible( int focusItemIndex )
488 VarTree::Iterator it;
489 int firstPosIndex = 0;
490 for( it = m_rTree.begin(); it != m_rTree.end();
491 it = m_rTree.getNextVisibleItem( it ) )
493 if( it == m_firstPos ) break;
497 if( it == m_rTree.end() ) return false;
500 if( it != m_rTree.end()
501 && ( focusItemIndex < firstPosIndex
502 || focusItemIndex > firstPosIndex + maxItems() ) )
504 // Scroll to have the wanted stream visible
505 VarPercent &rVarPos = m_rTree.getPositionVar();
506 rVarPos.set( 1.0 - (double)focusItemIndex /
507 (double)m_rTree.visibleItems() );
513 void CtrlTree::autoScroll()
515 // Find the current playing stream
517 VarTree::Iterator it;
519 for( it = m_rTree.begin(); it != m_rTree.end();
520 it = m_rTree.getNextItem( it ) )
524 m_rTree.ensureExpanded( it );
528 for( it = m_rTree.begin(); it != m_rTree.end();
529 it = m_rTree.getNextVisibleItem( it ) )
536 if( it == m_rTree.end() ) return;
539 ensureVisible( playIndex );
543 void CtrlTree::makeImage()
550 // Get the size of the control
551 const Position *pPos = getPosition();
556 int width = pPos->getWidth();
557 int height = pPos->getHeight();
559 int i_itemHeight = itemHeight();
562 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
563 m_pImage = pOsFactory->createOSGraphics( width, height );
565 VarTree::Iterator it = m_firstPos;
569 // Draw the background bitmap
570 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
571 m_pImage->drawBitmap( bmp, 0, 0 );
573 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
575 if( it != m_rTree.end() )
577 if( (*it).m_selected )
579 int rectHeight = __MIN( i_itemHeight, height - yPos );
580 m_pImage->fillRect( 0, yPos, width, rectHeight,
583 it = m_rTree.getNextVisibleItem( it );
590 // Fill background with background color
591 uint32_t bgColor = m_bgColor1;
592 m_pImage->fillRect( 0, 0, width, height, bgColor );
593 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
595 int rectHeight = __MIN( i_itemHeight, height - yPos );
596 if( it != m_rTree.end() )
598 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
599 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
600 it = m_rTree.getNextVisibleItem( it );
604 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
606 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
610 int bitmapWidth = itemImageWidth();
614 while( it != m_rTree.end() && yPos < height )
616 const GenericBitmap *m_pCurBitmap;
617 UString *pStr = (UString*)(it->m_cString.get());
618 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
623 int depth = it->depth();
624 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
630 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
632 m_pCurBitmap = m_pItemBitmap;
636 // Make sure we are centered on the line
637 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
638 if( yPos2 >= height )
643 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
644 bitmapWidth * (depth - 1 ), yPos2,
645 m_pCurBitmap->getWidth(),
646 __MIN( m_pCurBitmap->getHeight(),
647 height - yPos2), true );
649 yPos += i_itemHeight - pText->getHeight();
656 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
657 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
660 yPos += (pText->getHeight() - ySrc );
663 it = m_rTree.getNextVisibleItem( it );
665 /// \todo Reposition percentage var to accomodate if it's not suitable anymore (if we expanded a node)
668 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
670 // The first item is m_firstPos.
671 // We decrement pos as we try the other items, until pos == 0.
672 VarTree::Iterator it;
673 for( it = m_firstPos; it != m_rTree.end() && pos != 0;
674 it = m_rTree.getNextVisibleItem( it ) )