1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: ctrl_list.cpp 11009 2005-05-14 14:39:05Z ipkiss $
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
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,
58 CtrlGeneric( pIntf,rHelp, pVisible), m_rTree( rTree), m_rFont( rFont ),
59 m_pBgBitmap( pBgBitmap ), m_pItemBitmap( pItemBitmap ),
60 m_pOpenBitmap( pOpenBitmap ), m_pClosedBitmap( pClosedBitmap ),
61 m_fgColor( fgColor ), m_playColor( playColor ), m_bgColor1( bgColor1 ),
62 m_bgColor2( bgColor2 ), m_selColor( selColor ),
63 m_pLastSelected( NULL ), m_pImage( NULL )
65 // Observe the tree and position variables
66 m_rTree.addObserver( this );
67 m_rTree.getPositionVar().addObserver( this );
69 m_lastPos = m_rTree.begin();
76 m_rTree.getPositionVar().delObserver( this );
77 m_rTree.delObserver( this );
84 int CtrlTree::itemHeight()
86 int itemHeight = m_rFont.getSize();
89 itemHeight = __MAX( m_pClosedBitmap->getHeight(), itemHeight );
93 itemHeight = __MAX( m_pOpenBitmap->getHeight(), itemHeight );
97 itemHeight = __MAX( m_pItemBitmap->getHeight(), itemHeight );
99 itemHeight += LINE_INTERVAL;
103 int CtrlTree::itemImageWidth()
106 if( m_pClosedBitmap )
108 bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
112 bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
116 bitmapWidth = __MAX( m_pItemBitmap->getWidth(), bitmapWidth );
118 return bitmapWidth + 2;
121 int CtrlTree::maxItems()
123 const Position *pPos = getPosition();
128 return pPos->getHeight() / itemHeight();
132 void CtrlTree::onUpdate( Subject<VarTree> &rTree )
135 m_pLastSelected = NULL;
138 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
140 // Determine what is the first item to display
141 VarTree::Iterator it = m_rTree.begin();
143 int excessItems = m_rTree.visibleItems() - maxItems();
145 fprintf( stderr, "Hullo\n");
149 VarPercent &rVarPos = m_rTree.getPositionVar();
150 // a simple (int)(...) causes rounding errors !
154 it = m_rTree.visibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
156 if( m_lastPos != it )
158 fprintf( stderr, "updating\n" );
159 // Redraw the control if the position has changed
165 fprintf( stderr, "not updating\n" );
168 void CtrlTree::onResize()
170 // FIXME : shouldn't be the same as the onUpdate function ... but i'm lazy
171 // Determine what is the first item to display
172 VarTree::Iterator it = m_rTree.begin();
174 int excessItems = m_rTree.visibleItems() - maxItems();
178 VarPercent &rVarPos = m_rTree.getPositionVar();
179 // a simple (int)(...) causes rounding errors !
183 it = m_rTree.visibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
185 // Redraw the control if the position has changed
190 // Determine what is the first item to display
191 VarTree::Iterator it = m_rTree.begin();
193 int excessItems = m_rTree.visibleItems() - maxItems();
197 /* FIXME VarPercent &rVarPos = m_rTree.getPositionVar();
198 double newVal = 1.0 - (double)m_lastPos / excessItems;
201 // Change the position to keep the same first displayed item
202 rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
206 // We cannot keep the current first item
207 m_lastPos = excessItems;
209 it = m_rTree.visibleItem( excessItems );
216 void CtrlTree::onPositionChange()
222 #define IT_DISP_LOOP_END( a ) \
223 if( a ->m_expanded && a ->size() ) \
229 VarTree::Iterator it_old = a; \
231 if( it_old->parent() && it_old->parent()->end() == a ) \
233 a = it_old->uncle(); \
236 void CtrlTree::handleEvent( EvtGeneric &rEvent )
238 if( rEvent.getAsString().find( "key:down" ) != string::npos )
240 int key = ((EvtKey&)rEvent).getKey();
241 VarTree::Iterator it;
242 bool previousWasSelected = false;
243 for( it = m_rTree.begin(); it != m_rTree.end(); )
245 VarTree::Iterator next = it;
246 IT_DISP_LOOP_END( next );
251 && it != it->parent()->begin() )
252 || &*it != m_pLastSelected )
254 bool nextWasSelected = ( &*next == m_pLastSelected );
255 it->m_selected = nextWasSelected;
256 if( nextWasSelected )
258 m_pLastSelected = &*it;
262 else if( key == KEY_DOWN )
264 // Scroll down one item
266 && next != it->parent()->end() )
267 || &*it != m_pLastSelected )
269 (*it).m_selected = previousWasSelected;
271 if( previousWasSelected )
273 m_pLastSelected = &*it;
274 previousWasSelected = false;
278 previousWasSelected = ( &*it == m_pLastSelected );
281 else if( key == KEY_RIGHT )
283 // Go down one level (and expand node)
284 if( &*it == m_pLastSelected )
290 it->m_selected = false;
291 it->begin()->m_selected = true;
292 m_pLastSelected = &*(it->begin());
296 m_rTree.action( &*it );
301 it->m_expanded = true;
305 else if( key == KEY_LEFT )
307 // Go up one level (and close node)
308 if( &*it == m_pLastSelected )
310 if( it->m_expanded && it->size() )
312 it->m_expanded = false;
316 if( it->parent() && it->parent() != &m_rTree)
318 it->m_selected = false;
319 m_pLastSelected = it->parent();
320 m_pLastSelected->m_selected = true;
325 else if( key == KEY_ENTER || key == KEY_SPACE )
327 // Go up one level (and close node)
328 if( &*it == m_pLastSelected )
330 m_rTree.action( &*it );
336 // Redraw the control
341 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
343 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
344 const Position *pos = getPosition();
345 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
346 int xPos = rEvtMouse.getXPos() - pos->getLeft();
347 VarTree::Iterator it;
350 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
353 // Flag to know if the currend item must be selected
356 for( it = m_rTree.begin(); it != m_rTree.end(); )
358 bool nextSelect = select;
359 if( it == m_lastPos ) index = 0;
360 if( index == yPos || &*it == m_pLastSelected )
372 it->m_selected = (*it).m_selected || select;
376 IT_DISP_LOOP_END( it );
379 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
382 for( it = m_lastPos; it != m_rTree.end(); )
386 it->m_selected = !it->m_selected;
387 m_pLastSelected = &*it;
391 IT_DISP_LOOP_END( it );
394 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
397 // Flag to know if the currend item must be selected
400 for( it = m_rTree.begin(); it != m_rTree.end(); )
402 bool nextSelect = select;
403 if( it == m_lastPos ) index = 0;
404 if( index == yPos || &*it == m_pLastSelected )
416 it->m_selected = select;
420 IT_DISP_LOOP_END( it );
423 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
426 for( it = m_lastPos; it != m_rTree.end(); )
430 it->m_selected = true;
431 m_pLastSelected = &*it;
435 it->m_selected = false;
438 IT_DISP_LOOP_END( it );
442 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
445 for( it = m_lastPos; it != m_rTree.end(); )
449 if( it->size() && xPos < it->depth() * itemImageWidth() )
451 it->m_expanded = !it->m_expanded;
455 it->m_selected = true;
456 m_pLastSelected = &*it;
457 // Execute the action associated to this item
458 m_rTree.action( &*it );
463 it->m_selected = false;
466 IT_DISP_LOOP_END( it );
470 // Redraw the control
475 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
477 int direction = ((EvtScroll&)rEvent).getDirection();
479 double percentage = m_rTree.getPositionVar().get();
480 double step = 2.0 / (double)m_rTree.visibleItems();
481 if( direction == EvtScroll::kUp )
489 m_rTree.getPositionVar().set( percentage );
493 bool CtrlTree::mouseOver( int x, int y ) const
495 const Position *pPos = getPosition();
497 ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
501 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
505 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
509 void CtrlTree::autoScroll()
511 // Find the current playing stream
513 VarTree::Iterator it;
514 for( it = m_rTree.begin(); it != m_rTree.end(); )
516 if( it->m_playing ) break;
518 IT_DISP_LOOP_END( it );
521 if( it == m_rTree.end() ) return;
524 int lastPosIndex = 0;
525 for( it = m_rTree.begin(); it != m_rTree.end(); )
527 if( it == m_lastPos ) break;
529 IT_DISP_LOOP_END( it );
532 if( it == m_rTree.end() ) return;
535 if( it != m_rTree.end()
536 && ( playIndex < lastPosIndex
537 || playIndex > lastPosIndex + maxItems() ) )
539 // Scroll to have the playing stream visible
540 VarPercent &rVarPos = m_rTree.getPositionVar();
541 rVarPos.set( 1.0 - (double)playIndex / (double)m_rTree.visibleItems() );
550 void CtrlTree::makeImage()
557 // Get the size of the control
558 const Position *pPos = getPosition();
563 int width = pPos->getWidth();
564 int height = pPos->getHeight();
566 int i_itemHeight = itemHeight();
569 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
570 m_pImage = pOsFactory->createOSGraphics( width, height );
572 VarTree::Iterator it = m_lastPos;
576 // Draw the background bitmap
577 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
578 m_pImage->drawBitmap( bmp, 0, 0 );
580 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
582 int rectHeight = __MIN( i_itemHeight, height - yPos );
583 if( it != m_rTree.end() )
585 if( (*it).m_selected )
587 m_pImage->fillRect( 0, yPos, width, rectHeight,
590 IT_DISP_LOOP_END( it );
597 // Fill background with background color
598 uint32_t bgColor = m_bgColor1;
599 m_pImage->fillRect( 0, 0, width, height, bgColor );
600 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
602 int rectHeight = __MIN( i_itemHeight, height - yPos );
603 if( it != m_rTree.end() )
605 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
606 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
607 IT_DISP_LOOP_END( it );
611 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
613 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
616 // fprintf( stderr, "done\n");
618 int bitmapWidth = itemImageWidth();
622 while( it != m_rTree.end() && yPos < height )
624 const GenericBitmap *m_pCurBitmap;
625 UString *pStr = (UString*)(it->m_cString.get());
626 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
629 int depth = it->depth();
630 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
635 m_pCurBitmap = it->size() ? ( it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap ) : m_pItemBitmap ;
638 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
639 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0, bitmapWidth * (depth - 1 ), yPos2, m_pCurBitmap->getWidth(), __MIN( m_pCurBitmap->getHeight(), height - yPos2), true );
643 /* it would be nice to draw something */
645 yPos += i_itemHeight - pText->getHeight();
652 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
653 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
656 yPos += (pText->getHeight() - ySrc );
659 IT_DISP_LOOP_END( it );