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();
149 fprintf( stderr, "Hullo\n");
153 VarPercent &rVarPos = m_rTree.getPositionVar();
154 // a simple (int)(...) causes rounding errors !
158 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
160 if( m_lastPos != it )
162 fprintf( stderr, "updating\n" );
163 // Redraw the control if the position has changed
169 fprintf( stderr, "not updating\n" );
172 void CtrlTree::onResize()
174 // FIXME : shouldn't be the same as the onUpdate function ... but i'm lazy
175 // Determine what is the first item to display
176 VarTree::Iterator it = m_rTree.begin();
178 int excessItems = m_rTree.visibleItems() - maxItems();
182 VarPercent &rVarPos = m_rTree.getPositionVar();
183 // a simple (int)(...) causes rounding errors !
187 it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
189 // Redraw the control if the position has changed
194 // Determine what is the first item to display
195 VarTree::Iterator it = m_rTree.begin();
197 int excessItems = m_rTree.visibleItems() - maxItems();
201 /* FIXME VarPercent &rVarPos = m_rTree.getPositionVar();
202 double newVal = 1.0 - (double)m_lastPos / excessItems;
205 // Change the position to keep the same first displayed item
206 rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
210 // We cannot keep the current first item
211 m_lastPos = excessItems;
213 it = m_rTree.getVisibleItem( excessItems );
220 void CtrlTree::onPositionChange()
226 void CtrlTree::handleEvent( EvtGeneric &rEvent )
228 if( rEvent.getAsString().find( "key:down" ) != string::npos )
230 int key = ((EvtKey&)rEvent).getKey();
231 VarTree::Iterator it;
232 bool previousWasSelected = false;
233 for( it = m_rTree.begin(); it != m_rTree.end();
234 it = m_rTree.getNextVisibleItem( it ) )
236 VarTree::Iterator next = m_rTree.getNextVisibleItem( it );
239 // Scroll up one item
241 && it != it->parent()->begin() )
242 || &*it != m_pLastSelected )
244 bool nextWasSelected = ( &*next == m_pLastSelected );
245 it->m_selected = nextWasSelected;
246 if( nextWasSelected )
248 m_pLastSelected = &*it;
252 else if( key == KEY_DOWN )
254 // Scroll down one item
256 && next != it->parent()->end() )
257 || &*it != m_pLastSelected )
259 (*it).m_selected = previousWasSelected;
261 if( previousWasSelected )
263 m_pLastSelected = &*it;
264 previousWasSelected = false;
268 previousWasSelected = ( &*it == m_pLastSelected );
271 else if( key == KEY_RIGHT )
273 // Go down one level (and expand node)
274 if( &*it == m_pLastSelected )
280 it->m_selected = false;
281 it->begin()->m_selected = true;
282 m_pLastSelected = &*(it->begin());
286 m_rTree.action( &*it );
291 it->m_expanded = true;
295 else if( key == KEY_LEFT )
297 // Go up one level (and close node)
298 if( &*it == m_pLastSelected )
300 if( it->m_expanded && it->size() )
302 it->m_expanded = false;
306 if( it->parent() && it->parent() != &m_rTree)
308 it->m_selected = false;
309 m_pLastSelected = it->parent();
310 m_pLastSelected->m_selected = true;
315 else if( key == KEY_ENTER || key == KEY_SPACE )
317 // Go up one level (and close node)
318 if( &*it == m_pLastSelected )
320 m_rTree.action( &*it );
325 // Redraw the control
330 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
332 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
333 const Position *pos = getPosition();
334 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
335 int xPos = rEvtMouse.getXPos() - pos->getLeft();
336 VarTree::Iterator it;
338 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
341 VarTree::Iterator itClicked = findItemAtPos( yPos );
342 // Flag to know if the current item must be selected
344 for( it = m_rTree.begin(); it != m_rTree.end();
345 it = m_rTree.getNextVisibleItem( it ) )
347 bool nextSelect = select;
348 if( it == itClicked || &*it == m_pLastSelected )
360 it->m_selected = (*it).m_selected || select;
364 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
367 // Invert the selection of the item
368 it = findItemAtPos( yPos );
369 if( it != m_rTree.end() )
371 it->m_selected = !it->m_selected;
372 m_pLastSelected = &*it;
375 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
378 VarTree::Iterator itClicked = findItemAtPos( yPos );
379 // Flag to know if the current item must be selected
381 for( it = m_rTree.begin(); it != m_rTree.end();
382 it = m_rTree.getNextVisibleItem( it ) )
384 bool nextSelect = select;
385 if( it == itClicked || &*it == m_pLastSelected )
397 it->m_selected = select;
401 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
404 // Unselect any previously selected item
405 for( it = m_rTree.begin(); it != m_rTree.end();
406 it = m_rTree.getNextVisibleItem( it ) )
408 it->m_selected = false;
410 // Select the new item
411 it = findItemAtPos(yPos);
412 if( it != m_rTree.end() )
414 it->m_selected = true;
415 m_pLastSelected = &*it;
419 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
422 it = findItemAtPos(yPos);
423 if( it != m_rTree.end() )
425 if( it->size() && xPos < it->depth() * itemImageWidth() )
427 it->m_expanded = !it->m_expanded;
431 // Execute the action associated to this item
432 m_rTree.action( &*it );
437 // Redraw the control
442 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
444 int direction = ((EvtScroll&)rEvent).getDirection();
446 double percentage = m_rTree.getPositionVar().get();
447 double step = 2.0 / (double)m_rTree.visibleItems();
448 if( direction == EvtScroll::kUp )
456 m_rTree.getPositionVar().set( percentage );
460 bool CtrlTree::mouseOver( int x, int y ) const
462 const Position *pPos = getPosition();
464 ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
468 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
472 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
476 void CtrlTree::autoScroll()
478 // Find the current playing stream
480 VarTree::Iterator it;
481 for( it = m_rTree.begin(); it != m_rTree.end();
482 it = m_rTree.getNextVisibleItem( it ) )
484 if( it->m_playing ) break;
488 if( it == m_rTree.end() ) return;
491 int lastPosIndex = 0;
492 for( it = m_rTree.begin(); it != m_rTree.end();
493 it = m_rTree.getNextVisibleItem( it ) )
495 if( it == m_lastPos ) break;
499 if( it == m_rTree.end() ) return;
502 if( it != m_rTree.end()
503 && ( playIndex < lastPosIndex
504 || playIndex > lastPosIndex + maxItems() ) )
506 // Scroll to have the playing stream visible
507 VarPercent &rVarPos = m_rTree.getPositionVar();
508 rVarPos.set( 1.0 - (double)playIndex / (double)m_rTree.visibleItems() );
517 void CtrlTree::makeImage()
524 // Get the size of the control
525 const Position *pPos = getPosition();
530 int width = pPos->getWidth();
531 int height = pPos->getHeight();
533 int i_itemHeight = itemHeight();
536 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
537 m_pImage = pOsFactory->createOSGraphics( width, height );
539 VarTree::Iterator it = m_lastPos;
543 // Draw the background bitmap
544 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
545 m_pImage->drawBitmap( bmp, 0, 0 );
547 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
549 if( it != m_rTree.end() )
551 if( (*it).m_selected )
553 int rectHeight = __MIN( i_itemHeight, height - yPos );
554 m_pImage->fillRect( 0, yPos, width, rectHeight,
557 it = m_rTree.getNextVisibleItem( it );
564 // Fill background with background color
565 uint32_t bgColor = m_bgColor1;
566 m_pImage->fillRect( 0, 0, width, height, bgColor );
567 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
569 int rectHeight = __MIN( i_itemHeight, height - yPos );
570 if( it != m_rTree.end() )
572 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
573 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
574 it = m_rTree.getNextVisibleItem( it );
578 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
580 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
583 // fprintf( stderr, "done\n");
585 int bitmapWidth = itemImageWidth();
589 while( it != m_rTree.end() && yPos < height )
591 const GenericBitmap *m_pCurBitmap;
592 UString *pStr = (UString*)(it->m_cString.get());
593 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
597 int depth = it->depth();
598 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
604 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
606 m_pCurBitmap = m_pItemBitmap;
610 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
611 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
612 bitmapWidth * (depth - 1 ), yPos2,
613 m_pCurBitmap->getWidth(),
614 __MIN( m_pCurBitmap->getHeight(),
615 height - yPos2), true );
619 /* it would be nice to draw something */
621 yPos += i_itemHeight - pText->getHeight();
628 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
629 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
632 yPos += (pText->getHeight() - ySrc );
635 it = m_rTree.getNextVisibleItem( it );
639 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
641 // The first item is m_lastPos.
642 // We decrement pos as we try the other items, until pos == 0.
643 VarTree::Iterator it;
644 for( it = m_lastPos; it != m_rTree.end() && pos != 0;
645 it = m_rTree.getNextVisibleItem( it ) )