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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 # include "solaris_specific.h" // for lrint
42 #define SCROLL_STEP 0.05f
43 #define LINE_INTERVAL 1 // Number of pixels inserted between 2 lines
46 CtrlList::CtrlList( intf_thread_t *pIntf, VarList &rList,
47 const GenericFont &rFont, const GenericBitmap *pBitmap,
48 uint32_t fgColor, uint32_t playColor, uint32_t bgColor1,
49 uint32_t bgColor2, uint32_t selColor,
50 const UString &rHelp, VarBool *pVisible ):
51 CtrlGeneric( pIntf, rHelp, pVisible ), m_rList( rList ), m_rFont( rFont ),
52 m_pBitmap( pBitmap ), m_fgColor( fgColor ), m_playColor( playColor ),
53 m_bgColor1( bgColor1 ), m_bgColor2( bgColor2 ), m_selColor( selColor ),
54 m_pLastSelected( NULL ), m_pImage( NULL ), m_lastPos( 0 )
56 // Observe the list and position variables
57 m_rList.addObserver( this );
58 m_rList.getPositionVar().addObserver( this );
66 m_rList.getPositionVar().delObserver( this );
67 m_rList.delObserver( this );
75 void CtrlList::onUpdate( Subject<VarList, void*> &rList, void *arg )
78 m_pLastSelected = NULL;
82 void CtrlList::onUpdate( Subject<VarPercent, void*> &rPercent, void *arg )
84 // Get the size of the control
85 const Position *pPos = getPosition();
90 int height = pPos->getHeight();
92 // How many lines can be displayed ?
93 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
94 int maxItems = height / itemHeight;
96 // Determine what is the first item to display
97 VarPercent &rVarPos = m_rList.getPositionVar();
99 int excessItems = m_rList.size() - maxItems;
100 if( excessItems > 0 )
102 // a simple (int)(...) causes rounding errors !
106 firstItem = lrint( (1.0 - rVarPos.get()) * (double)excessItems );
108 if( m_lastPos != firstItem )
110 // Redraw the control if the position has changed
111 m_lastPos = firstItem;
118 void CtrlList::onResize()
120 // Get the size of the control
121 const Position *pPos = getPosition();
126 int height = pPos->getHeight();
128 // How many lines can be displayed ?
129 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
130 int maxItems = height / itemHeight;
132 // Update the position variable
133 VarPercent &rVarPos = m_rList.getPositionVar();
134 int excessItems = m_rList.size() - maxItems;
135 if( excessItems > 0 )
137 double newVal = 1.0 - (double)m_lastPos / excessItems;
140 // Change the position to keep the same first displayed item
141 rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
145 // We cannot keep the current first item
146 m_lastPos = excessItems;
155 void CtrlList::onPositionChange()
162 void CtrlList::handleEvent( EvtGeneric &rEvent )
164 if( rEvent.getAsString().find( "key:down" ) != string::npos )
166 int key = ((EvtKey&)rEvent).getKey();
167 VarList::Iterator it = m_rList.begin();
168 bool previousWasSelected = false;
169 while( it != m_rList.end() )
171 VarList::Iterator next = it;
175 // Scroll up one item
176 if( it != m_rList.begin() || &*it != m_pLastSelected )
178 bool nextWasSelected = ( &*next == m_pLastSelected );
179 (*it).m_selected = nextWasSelected;
180 if( nextWasSelected )
182 m_pLastSelected = &*it;
186 else if( key == KEY_DOWN )
188 // Scroll down one item
189 if( next != m_rList.end() || &*it != m_pLastSelected )
191 (*it).m_selected = previousWasSelected;
193 if( previousWasSelected )
195 m_pLastSelected = &*it;
196 previousWasSelected = false;
200 previousWasSelected = ( &*it == m_pLastSelected );
206 // Redraw the control
211 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
213 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
214 const Position *pos = getPosition();
215 int yPos = m_lastPos + ( rEvtMouse.getYPos() - pos->getTop() ) /
216 (m_rFont.getSize() + LINE_INTERVAL);
217 VarList::Iterator it;
220 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
223 // Flag to know if the current item must be selected
225 for( it = m_rList.begin(); it != m_rList.end(); it++ )
227 bool nextSelect = select;
228 if( index == yPos || &*it == m_pLastSelected )
240 (*it).m_selected = (*it).m_selected || select;
246 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
249 for( it = m_rList.begin(); it != m_rList.end(); it++ )
253 (*it).m_selected = ! (*it).m_selected;
254 m_pLastSelected = &*it;
261 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
264 // Flag to know if the current item must be selected
266 for( it = m_rList.begin(); it != m_rList.end(); it++ )
268 bool nextSelect = select;
269 if( index == yPos || &*it == m_pLastSelected )
281 (*it).m_selected = select;
287 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
290 for( it = m_rList.begin(); it != m_rList.end(); it++ )
294 (*it).m_selected = true;
295 m_pLastSelected = &*it;
299 (*it).m_selected = false;
305 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
308 for( it = m_rList.begin(); it != m_rList.end(); it++ )
312 (*it).m_selected = true;
313 m_pLastSelected = &*it;
314 // Execute the action associated to this item
315 m_rList.action( &*it );
319 (*it).m_selected = false;
325 // Redraw the control
330 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
332 int direction = ((EvtScroll&)rEvent).getDirection();
334 double percentage = m_rList.getPositionVar().get();
335 double step = 2.0 / (double)m_rList.size();
336 if( direction == EvtScroll::kUp )
344 m_rList.getPositionVar().set( percentage );
349 bool CtrlList::mouseOver( int x, int y ) const
351 const Position *pPos = getPosition();
354 int width = pPos->getWidth();
355 int height = pPos->getHeight();
356 return ( x >= 0 && x <= width && y >= 0 && y <= height );
362 void CtrlList::draw( OSGraphics &rImage, int xDest, int yDest )
366 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
371 void CtrlList::autoScroll()
373 // Get the size of the control
374 const Position *pPos = getPosition();
379 int height = pPos->getHeight();
381 // How many lines can be displayed ?
382 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
383 int maxItems = height / itemHeight;
385 // Find the current playing stream
387 VarList::ConstIterator it;
388 for( it = m_rList.begin(); it != m_rList.end(); it++ )
390 if( (*it).m_playing )
396 if( it != m_rList.end() &&
397 ( playIndex < m_lastPos || playIndex >= m_lastPos + maxItems ) )
399 // Scroll the list to have the playing stream visible
400 VarPercent &rVarPos = m_rList.getPositionVar();
401 rVarPos.set( 1.0 - (float)playIndex / (float)m_rList.size() );
402 // The image will be changed by onUpdate(VarPercent&)
412 void CtrlList::makeImage()
419 // Get the size of the control
420 const Position *pPos = getPosition();
425 int width = pPos->getWidth();
426 int height = pPos->getHeight();
427 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
430 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
431 m_pImage = pOsFactory->createOSGraphics( width, height );
433 VarList::ConstIterator it = m_rList[m_lastPos];
435 // Draw the background
438 // A background bitmap is given, so we scale it, ignoring the
440 ScaledBitmap bmp( getIntf(), *m_pBitmap, width, height );
441 m_pImage->drawBitmap( bmp, 0, 0 );
443 // Take care of the selection color
444 for( int yPos = 0; yPos < height; yPos += itemHeight )
446 int rectHeight = __MIN( itemHeight, height - yPos );
447 if( it != m_rList.end() )
449 if( (*it).m_selected )
451 m_pImage->fillRect( 0, yPos, width, rectHeight,
460 // No background bitmap, so use the 2 background colors
461 // Current background color
462 uint32_t bgColor = m_bgColor1;
463 for( int yPos = 0; yPos < height; yPos += itemHeight )
465 int rectHeight = __MIN( itemHeight, height - yPos );
466 if( it != m_rList.end() )
468 uint32_t color = ( (*it).m_selected ? m_selColor : bgColor );
469 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
474 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
476 // Flip the background color
477 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
483 for( it = m_rList[m_lastPos]; it != m_rList.end() && yPos < height; it++ )
485 UString *pStr = (UString*)(it->m_cString.get());
486 uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
489 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width );
494 yPos += itemHeight - pText->getHeight();
501 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
502 m_pImage->drawBitmap( *pText, 0, ySrc, 0, yPos, pText->getWidth(),
504 yPos += (pText->getHeight() - ySrc );