1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 #include "ctrl_list.hpp"
27 #include "../src/os_factory.hpp"
28 #include "../src/os_graphics.hpp"
29 #include "../src/generic_bitmap.hpp"
30 #include "../src/generic_font.hpp"
31 #include "../src/scaled_bitmap.hpp"
32 #include "../utils/position.hpp"
33 #include "../utils/ustring.hpp"
34 #include "../events/evt_key.hpp"
35 #include "../events/evt_mouse.hpp"
36 #include "../events/evt_scroll.hpp"
39 #define SCROLL_STEP 0.05f
40 #define LINE_INTERVAL 1 // Number of pixels inserted between 2 lines
43 CtrlList::CtrlList( intf_thread_t *pIntf, VarList &rList,
44 const GenericFont &rFont, const GenericBitmap *pBitmap,
45 uint32_t fgColor, uint32_t playColor, uint32_t bgColor1,
46 uint32_t bgColor2, uint32_t selColor,
47 const UString &rHelp, VarBool *pVisible ):
48 CtrlGeneric( pIntf, rHelp, pVisible ), m_rList( rList ), m_rFont( rFont ),
49 m_pBitmap( pBitmap ), m_fgColor( fgColor ), m_playColor( playColor ),
50 m_bgColor1( bgColor1 ), m_bgColor2( bgColor2 ), m_selColor( selColor ),
51 m_pLastSelected( NULL ), m_pImage( NULL ), m_lastPos( 0 )
53 // Observe the list and position variables
54 m_rList.addObserver( this );
55 m_rList.getPositionVar().addObserver( this );
63 m_rList.getPositionVar().delObserver( this );
64 m_rList.delObserver( this );
69 void CtrlList::onUpdate( Subject<VarList> &rList, void *arg )
72 m_pLastSelected = NULL;
76 void CtrlList::onUpdate( Subject<VarPercent> &rPercent, void *arg )
78 // Get the size of the control
79 const Position *pPos = getPosition();
84 int height = pPos->getHeight();
86 // How many lines can be displayed ?
87 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
88 int maxItems = height / itemHeight;
90 // Determine what is the first item to display
91 VarPercent &rVarPos = m_rList.getPositionVar();
93 int excessItems = m_rList.size() - maxItems;
96 // a simple (int)(...) causes rounding errors !
100 firstItem = lrint( (1.0 - rVarPos.get()) * (double)excessItems );
102 if( m_lastPos != firstItem )
104 // Redraw the control if the position has changed
105 m_lastPos = firstItem;
112 void CtrlList::onResize()
114 // Get the size of the control
115 const Position *pPos = getPosition();
120 int height = pPos->getHeight();
122 // How many lines can be displayed ?
123 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
124 int maxItems = height / itemHeight;
126 // Update the position variable
127 VarPercent &rVarPos = m_rList.getPositionVar();
128 int excessItems = m_rList.size() - maxItems;
129 if( excessItems > 0 )
131 double newVal = 1.0 - (double)m_lastPos / excessItems;
134 // Change the position to keep the same first displayed item
135 rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
139 // We cannot keep the current first item
140 m_lastPos = excessItems;
149 void CtrlList::onPositionChange()
156 void CtrlList::handleEvent( EvtGeneric &rEvent )
158 if( rEvent.getAsString().find( "key:down" ) != string::npos )
160 int key = ((EvtKey&)rEvent).getKey();
161 VarList::Iterator it = m_rList.begin();
162 bool previousWasSelected = false;
163 while( it != m_rList.end() )
165 VarList::Iterator next = it;
169 // Scroll up one item
170 if( it != m_rList.begin() || &*it != m_pLastSelected )
172 bool nextWasSelected = ( &*next == m_pLastSelected );
173 (*it).m_selected = nextWasSelected;
174 if( nextWasSelected )
176 m_pLastSelected = &*it;
180 else if( key == KEY_DOWN )
182 // Scroll down one item
183 if( next != m_rList.end() || &*it != m_pLastSelected )
185 (*it).m_selected = previousWasSelected;
187 if( previousWasSelected )
189 m_pLastSelected = &*it;
190 previousWasSelected = false;
194 previousWasSelected = ( &*it == m_pLastSelected );
200 // Redraw the control
205 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
207 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
208 const Position *pos = getPosition();
209 int yPos = m_lastPos + ( rEvtMouse.getYPos() - pos->getTop() ) /
210 (m_rFont.getSize() + LINE_INTERVAL);
211 VarList::Iterator it;
214 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
217 // Flag to know if the current item must be selected
219 for( it = m_rList.begin(); it != m_rList.end(); it++ )
221 bool nextSelect = select;
222 if( index == yPos || &*it == m_pLastSelected )
234 (*it).m_selected = (*it).m_selected || select;
240 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
243 for( it = m_rList.begin(); it != m_rList.end(); it++ )
247 (*it).m_selected = ! (*it).m_selected;
248 m_pLastSelected = &*it;
255 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
258 // Flag to know if the current item must be selected
260 for( it = m_rList.begin(); it != m_rList.end(); it++ )
262 bool nextSelect = select;
263 if( index == yPos || &*it == m_pLastSelected )
275 (*it).m_selected = select;
281 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
284 for( it = m_rList.begin(); it != m_rList.end(); it++ )
288 (*it).m_selected = true;
289 m_pLastSelected = &*it;
293 (*it).m_selected = false;
299 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
302 for( it = m_rList.begin(); it != m_rList.end(); it++ )
306 (*it).m_selected = true;
307 m_pLastSelected = &*it;
308 // Execute the action associated to this item
309 m_rList.action( &*it );
313 (*it).m_selected = false;
319 // Redraw the control
324 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
326 int direction = ((EvtScroll&)rEvent).getDirection();
328 double percentage = m_rList.getPositionVar().get();
329 double step = 2.0 / (double)m_rList.size();
330 if( direction == EvtScroll::kUp )
338 m_rList.getPositionVar().set( percentage );
343 bool CtrlList::mouseOver( int x, int y ) const
345 const Position *pPos = getPosition();
348 int width = pPos->getWidth();
349 int height = pPos->getHeight();
350 return ( x >= 0 && x <= width && y >= 0 && y <= height );
356 void CtrlList::draw( OSGraphics &rImage, int xDest, int yDest )
360 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
365 void CtrlList::autoScroll()
367 // Get the size of the control
368 const Position *pPos = getPosition();
373 int height = pPos->getHeight();
375 // How many lines can be displayed ?
376 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
377 int maxItems = height / itemHeight;
379 // Find the current playing stream
381 VarList::ConstIterator it;
382 for( it = m_rList.begin(); it != m_rList.end(); it++ )
384 if( (*it).m_playing )
390 if( it != m_rList.end() &&
391 ( playIndex < m_lastPos || playIndex >= m_lastPos + maxItems ) )
393 // Scroll the list to have the playing stream visible
394 VarPercent &rVarPos = m_rList.getPositionVar();
395 rVarPos.set( 1.0 - (float)playIndex / (float)m_rList.size() );
396 // The image will be changed by onUpdate(VarPercent&)
406 void CtrlList::makeImage()
410 // Get the size of the control
411 const Position *pPos = getPosition();
416 int width = pPos->getWidth();
417 int height = pPos->getHeight();
418 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
421 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
422 m_pImage = pOsFactory->createOSGraphics( width, height );
424 VarList::ConstIterator it = m_rList[m_lastPos];
426 // Draw the background
429 // A background bitmap is given, so we scale it, ignoring the
431 ScaledBitmap bmp( getIntf(), *m_pBitmap, width, height );
432 m_pImage->drawBitmap( bmp, 0, 0 );
434 // Take care of the selection color
435 for( int yPos = 0; yPos < height; yPos += itemHeight )
437 int rectHeight = __MIN( itemHeight, height - yPos );
438 if( it != m_rList.end() )
440 if( (*it).m_selected )
442 m_pImage->fillRect( 0, yPos, width, rectHeight,
451 // No background bitmap, so use the 2 background colors
452 // Current background color
453 uint32_t bgColor = m_bgColor1;
454 for( int yPos = 0; yPos < height; yPos += itemHeight )
456 int rectHeight = __MIN( itemHeight, height - yPos );
457 if( it != m_rList.end() )
459 uint32_t color = ( (*it).m_selected ? m_selColor : bgColor );
460 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
465 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
467 // Flip the background color
468 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
474 for( it = m_rList[m_lastPos]; it != m_rList.end() && yPos < height; it++ )
476 UString *pStr = (UString*)(it->m_cString.get());
477 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
480 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width );
485 yPos += itemHeight - pText->getHeight();
492 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
493 m_pImage->drawBitmap( *pText, 0, ySrc, 0, yPos, pText->getWidth(),
495 yPos += (pText->getHeight() - ySrc );