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::maxItems()
105 const Position *pPos = getPosition();
110 return pPos->getHeight() / itemHeight();
114 void CtrlTree::onUpdate( Subject<VarTree> &rTree )
117 m_pLastSelected = NULL;
120 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
122 // Determine what is the first item to display
123 VarTree::Iterator it = m_rTree.begin();
125 int excessItems = m_rTree.visibleItems() - maxItems();
129 VarPercent &rVarPos = m_rTree.getPositionVar();
130 // a simple (int)(...) causes rounding errors !
134 it = m_rTree.visibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1); /* FIXME : shouldn't need this +1 */
136 if( m_lastPos != it )
138 // Redraw the control if the position has changed
145 void CtrlTree::onResize()
147 // FIXME : shouldn't be the same as the onUpdate function ... but i'm lazy
148 // Determine what is the first item to display
149 VarTree::Iterator it = m_rTree.begin();
151 int excessItems = m_rTree.visibleItems() - maxItems();
155 VarPercent &rVarPos = m_rTree.getPositionVar();
156 // a simple (int)(...) causes rounding errors !
160 it = m_rTree.visibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1); /* FIXME : shouldn't need this +1 */
162 // Redraw the control if the position has changed
167 // Determine what is the first item to display
168 VarTree::Iterator it = m_rTree.begin();
170 int excessItems = m_rTree.visibleItems() - maxItems();
174 /* FIXME VarPercent &rVarPos = m_rTree.getPositionVar();
175 double newVal = 1.0 - (double)m_lastPos / excessItems;
178 // Change the position to keep the same first displayed item
179 rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
183 // We cannot keep the current first item
184 m_lastPos = excessItems;
186 it = m_rTree.visibleItem( excessItems );
193 void CtrlTree::onPositionChange()
199 #define IT_DISP_LOOP_END( a ) \
200 if( a ->m_expanded && a ->size() ) \
206 VarTree::Iterator it_old = a; \
208 if( it_old->parent() && it_old->parent()->end() == a ) \
210 a = it_old->uncle(); \
213 void CtrlTree::handleEvent( EvtGeneric &rEvent )
215 // TODO TODO FIXME TODO TODO
216 if( rEvent.getAsString().find( "key:down" ) != string::npos )
218 int key = ((EvtKey&)rEvent).getKey();
219 VarTree::Iterator it = m_rTree.begin();
220 bool previousWasSelected = false;
221 while( it != m_rTree.end() )
223 VarTree::Iterator next = it;
224 IT_DISP_LOOP_END( next );
229 && it != it->parent()->begin() )
230 || &*it != m_pLastSelected )
232 bool nextWasSelected = ( &*next == m_pLastSelected );
233 it->m_selected = nextWasSelected;
234 if( nextWasSelected )
236 m_pLastSelected = &*it;
240 else if( key == KEY_DOWN )
242 // Scroll down one item
244 && next != it->parent()->end() )
245 || &*it != m_pLastSelected )
247 (*it).m_selected = previousWasSelected;
249 if( previousWasSelected )
251 m_pLastSelected = &*it;
252 previousWasSelected = false;
256 previousWasSelected = ( &*it == m_pLastSelected );
259 else if( key == KEY_RIGHT )
267 m_pLastSelected = &*(it->begin());
272 it->m_expanded = true;
275 else if( key == KEY_LEFT )
277 // Go up one level (and close node)
279 it->m_expanded = false;
284 // Redraw the control
289 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
291 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
292 const Position *pos = getPosition();
293 int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
294 VarTree::Iterator it;
297 // TODO : add all other mouse controls
298 /**/ if( rEvent.getAsString().find( "mouse:left:down" ) !=
301 for( it = m_lastPos; it != m_rTree.end(); )
305 it->m_selected = true;
306 m_pLastSelected = &*it;
310 it->m_selected = false;
313 IT_DISP_LOOP_END( it );
317 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
320 for( it = m_lastPos; it != m_rTree.end(); )
324 it->m_selected = true;
325 m_pLastSelected = &*it;
326 // Execute the action associated to this item
327 m_rTree.action( &*it );
331 it->m_selected = false;
334 IT_DISP_LOOP_END( it );
338 // Redraw the control
343 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
345 int direction = ((EvtScroll&)rEvent).getDirection();
347 double percentage = m_rTree.getPositionVar().get();
348 if( direction == EvtScroll::kUp )
350 percentage += SCROLL_STEP;
354 percentage -= SCROLL_STEP;
356 m_rTree.getPositionVar().set( percentage );
360 bool CtrlTree::mouseOver( int x, int y ) const
362 const Position *pPos = getPosition();
364 ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
368 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
372 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
376 void CtrlTree::autoScroll()
383 void CtrlTree::makeImage()
390 // Get the size of the control
391 const Position *pPos = getPosition();
396 int width = pPos->getWidth();
397 int height = pPos->getHeight();
399 int i_itemHeight = itemHeight();
402 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
403 m_pImage = pOsFactory->createOSGraphics( width, height );
405 VarTree::Iterator it = m_lastPos;
409 // Draw the background bitmap
410 ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
411 m_pImage->drawBitmap( bmp, 0, 0 );
413 // FIXME : Take care of the selection color
414 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
416 int rectHeight = __MIN( i_itemHeight, height - yPos );
417 if( it != m_rTree.end() )
419 if( (*it).m_selected )
421 m_pImage->fillRect( 0, yPos, width, rectHeight,
424 IT_DISP_LOOP_END( it );
431 // Fill background with background color
432 uint32_t bgColor = m_bgColor1;
433 m_pImage->fillRect( 0, 0, width, height, bgColor );
434 for( int yPos = 0; yPos < height; yPos += i_itemHeight )
436 int rectHeight = __MIN( i_itemHeight, height - yPos );
437 if( it != m_rTree.end() )
439 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
440 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
441 IT_DISP_LOOP_END( it );
445 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
447 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
450 // fprintf( stderr, "done\n");
453 if( m_pClosedBitmap )
455 bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
459 bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
463 bitmapWidth = __MAX( m_pItemBitmap->getWidth(), bitmapWidth );
467 // FIXME : Draw the items
470 while( it != m_rTree.end() && yPos < height )
472 const GenericBitmap *m_pCurBitmap;
473 UString *pStr = (UString*)(it->m_cString.get());
474 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
477 int depth = it->depth();
478 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
483 m_pCurBitmap = it->size() ? ( it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap ) : m_pItemBitmap ;
486 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
487 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0, bitmapWidth * (depth - 1 ), yPos2, m_pCurBitmap->getWidth(), __MIN( m_pCurBitmap->getHeight(), height - yPos2), true );
491 /* it would be nice to draw something */
493 yPos += i_itemHeight - pText->getHeight();
500 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
501 m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
504 yPos += (pText->getHeight() - ySrc );
507 IT_DISP_LOOP_END( it );