1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
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 "../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 #define SCROLL_STEP 0.05
39 #define LINE_INTERVAL 1 // Number of pixels inserted between 2 lines
42 CtrlList::CtrlList( intf_thread_t *pIntf, VarList &rList, GenericFont &rFont,
43 uint32_t fgColor, uint32_t playColor, uint32_t bgColor1,
44 uint32_t bgColor2, uint32_t selColor,
45 const UString &rHelp, VarBool *pVisible ):
46 CtrlGeneric( pIntf, rHelp, pVisible ), m_rList( rList ), m_rFont( rFont ),
47 m_fgColor( fgColor ), m_playColor( playColor ), m_bgColor1( bgColor1 ),
48 m_bgColor2( bgColor2 ), m_selColor( selColor ), m_pLastSelected( NULL ),
49 m_pImage( NULL ), m_lastPos( 0 )
51 // Observe the list and position variables
52 m_rList.addObserver( this );
53 m_rList.getPositionVar().addObserver( this );
61 m_rList.getPositionVar().delObserver( this );
62 m_rList.delObserver( this );
70 void CtrlList::onUpdate( Subject<VarList> &rList )
74 m_pLastSelected = NULL;
78 void CtrlList::onUpdate( Subject<VarPercent> &rPercent )
80 // Get the size of the control
81 const Position *pPos = getPosition();
86 int height = pPos->getHeight();
88 // How many lines can be displayed ?
89 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
90 int maxItems = height / itemHeight;
92 // Determine what is the first item to display
93 VarPercent &rVarPos = m_rList.getPositionVar();
95 int excessItems = m_rList.size() - maxItems;
98 // a simple (int)(...) causes rounding errors !
102 firstItem = lrint( (1.0 - rVarPos.get()) * (double)excessItems );
104 if( m_lastPos != firstItem )
106 // Redraw the control if the position has changed
107 m_lastPos = firstItem;
114 void CtrlList::onResize()
116 // Get the size of the control
117 const Position *pPos = getPosition();
122 int height = pPos->getHeight();
124 // How many lines can be displayed ?
125 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
126 int maxItems = height / itemHeight;
128 // Update the position variable
129 VarPercent &rVarPos = m_rList.getPositionVar();
130 int excessItems = m_rList.size() - maxItems;
131 if( excessItems > 0 )
133 double newVal = 1.0 - (double)m_lastPos / excessItems;
136 // Change the position to keep the same first displayed item
137 rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
141 // We cannot keep the current first item
142 m_lastPos = excessItems;
151 void CtrlList::onPositionChange()
158 void CtrlList::handleEvent( EvtGeneric &rEvent )
160 if( rEvent.getAsString().find( "key:down" ) != string::npos )
162 char key = ((EvtKey&)rEvent).getKey();
165 else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
167 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
168 const Position *pos = getPosition();
169 int yPos = m_lastPos + ( rEvtMouse.getYPos() - pos->getTop() ) /
170 (m_rFont.getSize() + LINE_INTERVAL);
171 VarList::Iterator it;
174 if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
177 // Flag to know if the current item must be selected
179 for( it = m_rList.begin(); it != m_rList.end(); it++ )
181 bool nextSelect = select;
182 if( index == yPos || &*it == m_pLastSelected )
194 (*it).m_selected = (*it).m_selected || select;
200 else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
203 for( it = m_rList.begin(); it != m_rList.end(); it++ )
207 (*it).m_selected = ! (*it).m_selected;
208 m_pLastSelected = &*it;
215 else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
218 // Flag to know if the current item must be selected
220 for( it = m_rList.begin(); it != m_rList.end(); it++ )
222 bool nextSelect = select;
223 if( index == yPos || &*it == m_pLastSelected )
235 (*it).m_selected = select;
241 else if( rEvent.getAsString().find( "mouse:left:down" ) !=
244 for( it = m_rList.begin(); it != m_rList.end(); it++ )
248 (*it).m_selected = true;
249 m_pLastSelected = &*it;
253 (*it).m_selected = false;
259 else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
262 for( it = m_rList.begin(); it != m_rList.end(); it++ )
266 (*it).m_selected = true;
267 m_pLastSelected = &*it;
268 // Execute the action associated to this item
269 m_rList.action( &*it );
273 (*it).m_selected = false;
279 // Redraw the control
284 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
286 int direction = ((EvtScroll&)rEvent).getDirection();
288 double percentage = m_rList.getPositionVar().get();
289 if( direction == EvtScroll::kUp )
291 percentage += SCROLL_STEP;
295 percentage -= SCROLL_STEP;
297 m_rList.getPositionVar().set( percentage );
302 bool CtrlList::mouseOver( int x, int y ) const
304 const Position *pPos = getPosition();
307 int width = pPos->getWidth();
308 int height = pPos->getHeight();
309 return ( x >= 0 && x <= width && y >= 0 && y <= height );
315 void CtrlList::draw( OSGraphics &rImage, int xDest, int yDest )
319 rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
324 void CtrlList::makeImage()
331 // Get the size of the control
332 const Position *pPos = getPosition();
337 int width = pPos->getWidth();
338 int height = pPos->getHeight();
339 int itemHeight = m_rFont.getSize() + LINE_INTERVAL;
342 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
343 m_pImage = pOsFactory->createOSGraphics( width, height );
345 // Current background color
346 uint32_t bgColor = m_bgColor1;
348 // Draw the background
349 VarList::ConstIterator it = m_rList[m_lastPos];
350 for( int yPos = 0; yPos < height; yPos += itemHeight )
352 int rectHeight = __MIN( itemHeight, height - yPos );
353 if( it != m_rList.end() )
355 uint32_t color = ( (*it).m_selected ? m_selColor : bgColor );
356 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
361 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
363 // Flip the background color
364 bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
369 for( it = m_rList[m_lastPos]; it != m_rList.end() && yPos < height; it++ )
371 UString *pStr = (UString*)((*it).m_cString.get());
372 uint32_t color = ( (*it).m_playing ? m_playColor : m_fgColor );
375 GenericBitmap *pText = m_rFont.drawString( *pStr, color, width );
380 yPos += itemHeight - pText->getHeight();
387 int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
388 m_pImage->drawBitmap( *pText, 0, ySrc, 0, yPos, pText->getWidth(),
390 yPos += (pText->getHeight() - ySrc );