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., 59 Temple Place - Suite 330, Boston, MA 02111, 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_lastPos = 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> &rTree )
135 // Invalidate the position when the tree is updated
136 m_lastPos = m_rTree.begin();
139 m_pLastSelected = NULL;
142 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
144 // Determine what is the first item to display
145 VarTree::Iterator it = m_rTree.begin();
147 int excessItems = m_rTree.visibleItems() - maxItems();
151 VarPercent &rVarPos = m_rTree.getPositionVar();
152 // a simple (int)(...) causes rounding errors !
156 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
158 if( m_lastPos != it )
160 // Redraw the control if the position has changed
167 void CtrlTree::onResize()
169 // FIXME : shouldn't be the same as the onUpdate function ... but i'm lazy
170 // Determine what is the first item to display
171 VarTree::Iterator it = m_rTree.begin();
173 int excessItems = m_rTree.visibleItems() - maxItems();
177 VarPercent &rVarPos = m_rTree.getPositionVar();
178 // a simple (int)(...) causes rounding errors !
182 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
184 // Redraw the control if the position has changed
189 // Determine what is the first item to display
190 VarTree::Iterator it = m_rTree.begin();
192 int excessItems = m_rTree.visibleItems() - maxItems();
196 /* FIXME VarPercent &rVarPos = m_rTree.getPositionVar();
197 double newVal = 1.0 - (double)m_lastPos / excessItems;
200 // Change the position to keep the same first displayed item
201 rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
205 // We cannot keep the current first item
206 m_lastPos = excessItems;
208 it = m_rTree.getVisibleItem( excessItems );
215 void CtrlTree::onPositionChange()
221 void CtrlTree::handleEvent( EvtGeneric &rEvent )
223 if( rEvent.getAsString().find( "key:down" ) != string::npos )
225 int key = ((EvtKey&)rEvent).getKey();
226 VarTree::Iterator it;
227 bool previousWasSelected = false;
228 for( it = m_rTree.begin(); it != m_rTree.end();
229 it = m_rTree.getNextVisibleItem( it ) )
231 VarTree::Iterator next = m_rTree.getNextVisibleItem( it );
234 // Scroll up one item
236 && it != it->parent()->begin() )
237 || &*it != m_pLastSelected )
239 bool nextWasSelected = ( &*next == m_pLastSelected );
240 it->m_selected = nextWasSelected;
241 if( nextWasSelected )
243 m_pLastSelected = &*it;
247 else if( key == KEY_DOWN )
249 // Scroll down one item
251 && next != it->parent()->end() )
252 || &*it != m_pLastSelected )
254 (*it).m_selected = previousWasSelected;
256 if( previousWasSelected )
258 m_pLastSelected = &*it;
259 previousWasSelected = false;
263 previousWasSelected = ( &*it == m_pLastSelected );
266 else if( key == KEY_RIGHT )
268 // Go down one level (and expand node)
269 if( &*it == m_pLastSelected )
275 it->m_selected = false;
276 it->begin()->m_selected = true;
277 m_pLastSelected = &*(it->begin());
281 m_rTree.action( &*it );
286 it->m_expanded = true;
290 else if( key == KEY_LEFT )
292 // Go up one level (and close node)
293 if( &*it == m_pLastSelected )
295 if( it->m_expanded && it->size() )
297 it->m_expanded = false;
301 if( it->parent() && it->parent() != &m_rTree)
303 it->m_selected = false;
304 m_pLastSelected = it->parent();
305 m_pLastSelected->m_selected = true;
310 else if( key == KEY_ENTER || key == KEY_SPACE )
312 // Go up one level (and close node)
313 if( &*it == m_pLastSelected )
315 m_rTree.action( &*it );
320 // Redraw the control
325 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
327 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
328 const Position *pos = getPosition();
329 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
330 int xPos = rEvtMouse.getXPos() - pos->getLeft();
331 VarTree::Iterator it;
333 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
336 VarTree::Iterator itClicked = findItemAtPos( yPos );
337 // Flag to know if the current item must be selected
339 for( it = m_rTree.begin(); it != m_rTree.end();
340 it = m_rTree.getNextVisibleItem( it ) )
342 bool nextSelect = select;
343 if( it == itClicked || &*it == m_pLastSelected )
355 it->m_selected = (*it).m_selected || select;
359 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
362 // Invert the selection of the item
363 it = findItemAtPos( yPos );
364 if( it != m_rTree.end() )
366 it->m_selected = !it->m_selected;
367 m_pLastSelected = &*it;
370 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
373 VarTree::Iterator itClicked = findItemAtPos( yPos );
374 // Flag to know if the current item must be selected
376 for( it = m_rTree.begin(); it != m_rTree.end();
377 it = m_rTree.getNextVisibleItem( it ) )
379 bool nextSelect = select;
380 if( it == itClicked || &*it == m_pLastSelected )
392 it->m_selected = select;
396 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
399 // Unselect any previously selected item
400 for( it = m_rTree.begin(); it != m_rTree.end();
401 it = m_rTree.getNextVisibleItem( it ) )
403 it->m_selected = false;
405 // Select the new item
406 it = findItemAtPos(yPos);
407 if( it != m_rTree.end() )
409 it->m_selected = true;
410 m_pLastSelected = &*it;
414 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
417 it = findItemAtPos(yPos);
418 if( it != m_rTree.end() )
420 if( it->size() && xPos < it->depth() * itemImageWidth() )
422 it->m_expanded = !it->m_expanded;
426 // Execute the action associated to this item
427 m_rTree.action( &*it );
432 // Redraw the control
437 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
439 int direction = ((EvtScroll&)rEvent).getDirection();
441 double percentage = m_rTree.getPositionVar().get();
442 double step = 2.0 / (double)m_rTree.visibleItems();
443 if( direction == EvtScroll::kUp )
451 m_rTree.getPositionVar().set( percentage );
455 bool CtrlTree::mouseOver( int x, int y ) const
457 const Position *pPos = getPosition();
459 ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
463 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
467 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
471 void CtrlTree::autoScroll()
473 // Find the current playing stream
475 VarTree::Iterator it;
476 for( it = m_rTree.begin(); it != m_rTree.end();
477 it = m_rTree.getNextVisibleItem( it ) )
479 if( it->m_playing ) break;
483 if( it == m_rTree.end() ) return;
486 int lastPosIndex = 0;
487 for( it = m_rTree.begin(); it != m_rTree.end();
488 it = m_rTree.getNextVisibleItem( it ) )
490 if( it == m_lastPos ) break;
494 if( it == m_rTree.end() ) return;
497 if( it != m_rTree.end()
498 && ( playIndex < lastPosIndex
499 || playIndex > lastPosIndex + maxItems() ) )
501 // Scroll to have the playing stream visible
502 VarPercent &rVarPos = m_rTree.getPositionVar();
503 rVarPos.set( 1.0 - (double)playIndex / (double)m_rTree.visibleItems() );
512 void CtrlTree::makeImage()
519 // Get the size of the control
520 const Position *pPos = getPosition();
525 int width = pPos->getWidth();
526 int height = pPos->getHeight();
528 int i_itemHeight = itemHeight();
531 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
532 m_pImage = pOsFactory->createOSGraphics( width, height );
534 VarTree::Iterator it = m_lastPos;
538 // Draw the background bitmap
539 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
540 m_pImage->drawBitmap( bmp, 0, 0 );
542 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
544 if( it != m_rTree.end() )
546 if( (*it).m_selected )
548 int rectHeight = __MIN( i_itemHeight, height - yPos );
549 m_pImage->fillRect( 0, yPos, width, rectHeight,
552 it = m_rTree.getNextVisibleItem( it );
559 // Fill background with background color
560 uint32_t bgColor = m_bgColor1;
561 m_pImage->fillRect( 0, 0, width, height, bgColor );
562 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
564 int rectHeight = __MIN( i_itemHeight, height - yPos );
565 if( it != m_rTree.end() )
567 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
568 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
569 it = m_rTree.getNextVisibleItem( it );
573 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
575 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
578 // fprintf( stderr, "done\n");
580 int bitmapWidth = itemImageWidth();
584 while( it != m_rTree.end() && yPos < height )
586 const GenericBitmap *m_pCurBitmap;
587 UString *pStr = (UString*)(it->m_cString.get());
588 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
592 int depth = it->depth();
593 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
599 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
601 m_pCurBitmap = m_pItemBitmap;
605 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
606 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
607 bitmapWidth * (depth - 1 ), yPos2,
608 m_pCurBitmap->getWidth(),
609 __MIN( m_pCurBitmap->getHeight(),
610 height - yPos2), true );
614 /* it would be nice to draw something */
616 yPos += i_itemHeight - pText->getHeight();
623 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
624 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
627 yPos += (pText->getHeight() - ySrc );
630 it = m_rTree.getNextVisibleItem( it );
634 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
636 // The first item is m_lastPos.
637 // We decrement pos as we try the other items, until pos == 0.
638 VarTree::Iterator it;
639 for( it = m_lastPos; it != m_rTree.end() && pos != 0;
640 it = m_rTree.getNextVisibleItem( it ) )