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 *****************************************************************************/
25 #include "ctrl_slider.hpp"
26 #include "../events/evt_enter.hpp"
27 #include "../events/evt_mouse.hpp"
28 #include "../events/evt_scroll.hpp"
29 #include "../src/generic_bitmap.hpp"
30 #include "../src/top_window.hpp"
31 #include "../src/os_factory.hpp"
32 #include "../src/os_graphics.hpp"
33 #include "../utils/position.hpp"
34 #include "../utils/var_percent.hpp"
38 #define SCROLL_STEP 0.05
41 CtrlSliderCursor::CtrlSliderCursor( intf_thread_t *pIntf,
42 const GenericBitmap &rBmpUp,
43 const GenericBitmap &rBmpOver,
44 const GenericBitmap &rBmpDown,
46 VarPercent &rVariable,
48 const UString &rTooltip,
49 const UString &rHelp ):
50 CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
51 m_rVariable( rVariable ), m_tooltip( rTooltip ),
52 m_width( rCurve.getWidth() ), m_height( rCurve.getHeight() ),
53 m_cmdOverDown( this, &transOverDown ),
54 m_cmdDownOver( this, &transDownOver ), m_cmdOverUp( this, &transOverUp ),
55 m_cmdUpOver( this, &transUpOver ), m_cmdMove( this, &transMove ),
56 m_cmdScroll( this, &transScroll ),
57 m_lastPercentage( 0 ), m_xOffset( 0 ), m_yOffset( 0 ),
58 m_pEvt( NULL ), m_rCurve( rCurve )
60 // Build the images of the cursor
61 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
62 m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
64 m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
65 m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
66 rBmpDown.getHeight() );
67 m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
68 m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
69 rBmpOver.getHeight() );
70 m_pImgOver->drawBitmap( rBmpOver, 0, 0 );
73 m_fsm.addState( "up" );
74 m_fsm.addState( "over" );
75 m_fsm.addState( "down" );
78 m_fsm.addTransition( "over", "mouse:left:down", "down",
80 m_fsm.addTransition( "down", "mouse:left:up", "over",
82 m_fsm.addTransition( "over", "leave", "up", &m_cmdOverUp );
83 m_fsm.addTransition( "up", "enter", "over", &m_cmdUpOver );
84 m_fsm.addTransition( "down", "motion", "down", &m_cmdMove );
85 m_fsm.addTransition( "over", "scroll", "over", &m_cmdScroll );
88 m_fsm.setState( "up" );
91 // Observe the position variable
92 m_rVariable.addObserver( this );
94 // Initial position of the cursor
95 m_lastPercentage = m_rVariable.get();
99 CtrlSliderCursor::~CtrlSliderCursor()
101 m_rVariable.delObserver( this );
102 SKINS_DELETE( m_pImgUp );
103 SKINS_DELETE( m_pImgDown );
104 SKINS_DELETE( m_pImgOver );
108 void CtrlSliderCursor::handleEvent( EvtGeneric &rEvent )
110 // Save the event to use it in callbacks
113 m_fsm.handleTransition( rEvent.getAsString() );
117 bool CtrlSliderCursor::mouseOver( int x, int y ) const
121 // Compute the position of the cursor
123 m_rCurve.getPoint( m_rVariable.get(), xPos, yPos );
125 // Compute the resize factors
126 float factorX, factorY;
127 getResizeFactors( factorX, factorY );
128 xPos = (int)(xPos * factorX);
129 yPos = (int)(yPos * factorY);
131 return m_pImg->hit( x - xPos + m_pImg->getWidth() / 2,
132 y - yPos + m_pImg->getHeight() / 2 );
141 void CtrlSliderCursor::draw( OSGraphics &rImage, int xDest, int yDest )
145 // Compute the position of the cursor
147 m_rCurve.getPoint( m_rVariable.get(), xPos, yPos );
149 // Compute the resize factors
150 float factorX, factorY;
151 getResizeFactors( factorX, factorY );
152 xPos = (int)(xPos * factorX);
153 yPos = (int)(yPos * factorY);
155 // Draw the current image
156 rImage.drawGraphics( *m_pImg, 0, 0,
157 xDest + xPos - m_pImg->getWidth() / 2,
158 yDest + yPos - m_pImg->getHeight() / 2 );
163 void CtrlSliderCursor::onUpdate( Subject<VarPercent> &rVariable )
165 // The position has changed
166 notifyLayout( m_rCurve.getWidth(), m_rCurve.getHeight() );
170 void CtrlSliderCursor::transOverDown( SkinObject *pCtrl )
172 CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
173 EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
175 // Compute the resize factors
176 float factorX, factorY;
177 pThis->getResizeFactors( factorX, factorY );
179 // Get the position of the control
180 const Position *pPos = pThis->getPosition();
182 // Compute the offset
184 pThis->m_rCurve.getPoint( pThis->m_rVariable.get(), tempX, tempY );
185 pThis->m_xOffset = pEvtMouse->getXPos() - pPos->getLeft()
186 - (int)(tempX * factorX);
187 pThis->m_yOffset = pEvtMouse->getYPos() - pPos->getTop()
188 - (int)(tempY * factorY);
190 pThis->captureMouse();
191 pThis->m_pImg = pThis->m_pImgDown;
192 pThis->notifyLayout( pThis->m_rCurve.getWidth(),
193 pThis->m_rCurve.getHeight() );
197 void CtrlSliderCursor::transDownOver( SkinObject *pCtrl )
199 CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
202 pThis->m_lastPercentage = pThis->m_rVariable.get();
204 pThis->releaseMouse();
205 pThis->m_pImg = pThis->m_pImgUp;
206 pThis->notifyLayout( pThis->m_rCurve.getWidth(),
207 pThis->m_rCurve.getHeight() );
211 void CtrlSliderCursor::transUpOver( SkinObject *pCtrl )
213 CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
215 pThis->m_pImg = pThis->m_pImgOver;
216 pThis->notifyLayout( pThis->m_rCurve.getWidth(),
217 pThis->m_rCurve.getHeight() );
221 void CtrlSliderCursor::transOverUp( SkinObject *pCtrl )
223 CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
225 pThis->m_pImg = pThis->m_pImgUp;
226 pThis->notifyLayout( pThis->m_rCurve.getWidth(),
227 pThis->m_rCurve.getHeight() );
231 void CtrlSliderCursor::transMove( SkinObject *pCtrl )
233 CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
234 EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
236 // Get the position of the control
237 const Position *pPos = pThis->getPosition();
239 // Compute the resize factors
240 float factorX, factorY;
241 pThis->getResizeFactors( factorX, factorY );
243 // Compute the relative position of the centre of the cursor
244 float relX = pEvtMouse->getXPos() - pPos->getLeft() - pThis->m_xOffset;
245 float relY = pEvtMouse->getYPos() - pPos->getTop() - pThis->m_yOffset;
246 // Ponderate with the resize factors
247 int relXPond = (int)(relX / factorX);
248 int relYPond = (int)(relY / factorY);
250 // Update the position
251 if( pThis->m_rCurve.getMinDist( relXPond, relYPond ) < RANGE )
253 float percentage = pThis->m_rCurve.getNearestPercent( relXPond,
255 pThis->m_rVariable.set( percentage );
259 pThis->m_rVariable.set( pThis->m_lastPercentage );
263 void CtrlSliderCursor::transScroll( SkinObject *pCtrl )
265 CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
266 EvtScroll *pEvtScroll = (EvtScroll*)pThis->m_pEvt;
268 int direction = pEvtScroll->getDirection();
270 float percentage = pThis->m_rVariable.get();
271 if( direction == EvtScroll::kUp )
273 percentage += SCROLL_STEP;
277 percentage -= SCROLL_STEP;
280 pThis->m_rVariable.set( percentage );
284 void CtrlSliderCursor::getResizeFactors( float &rFactorX,
285 float &rFactorY ) const
287 // Get the position of the control
288 const Position *pPos = getPosition();
293 // Compute the resize factors
296 rFactorX = (float)pPos->getWidth() / (float)m_width;
300 rFactorY = (float)pPos->getHeight() / (float)m_height;
306 CtrlSliderBg::CtrlSliderBg( intf_thread_t *pIntf, CtrlSliderCursor &rCursor,
307 const Bezier &rCurve, VarPercent &rVariable,
308 int thickness, VarBool *pVisible,
309 const UString &rHelp ):
310 CtrlGeneric( pIntf, rHelp, pVisible ), m_rCursor( rCursor ),
311 m_rVariable( rVariable ), m_thickness( thickness ), m_rCurve( rCurve ),
312 m_width( rCurve.getWidth() ), m_height( rCurve.getHeight() )
317 bool CtrlSliderBg::mouseOver( int x, int y ) const
319 // Compute the resize factors
320 float factorX, factorY;
321 getResizeFactors( factorX, factorY );
323 return (m_rCurve.getMinDist( (int)(x / factorX),
324 (int)(y / factorY) ) < m_thickness );
328 void CtrlSliderBg::handleEvent( EvtGeneric &rEvent )
330 if( rEvent.getAsString().find( "mouse:left:down" ) != string::npos )
332 // Compute the resize factors
333 float factorX, factorY;
334 getResizeFactors( factorX, factorY );
336 // Get the position of the control
337 const Position *pPos = getPosition();
339 // Get the value corresponding to the position of the mouse
340 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
341 int x = rEvtMouse.getXPos();
342 int y = rEvtMouse.getYPos();
343 m_rVariable.set( m_rCurve.getNearestPercent(
344 (int)((x - pPos->getLeft()) / factorX),
345 (int)((y - pPos->getTop()) / factorY) ) );
347 // Forward the clic to the cursor
348 EvtMouse evt( getIntf(), x, y, EvtMouse::kLeft, EvtMouse::kDown );
349 TopWindow *pWin = getWindow();
352 EvtEnter evtEnter( getIntf() );
353 // XXX It was not supposed to be implemented like that !!
354 pWin->forwardEvent( evtEnter, m_rCursor );
355 pWin->forwardEvent( evt, m_rCursor );
358 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
360 int direction = ((EvtScroll&)rEvent).getDirection();
362 float percentage = m_rVariable.get();
363 if( direction == EvtScroll::kUp )
365 percentage += SCROLL_STEP;
369 percentage -= SCROLL_STEP;
372 m_rVariable.set( percentage );
377 void CtrlSliderBg::getResizeFactors( float &rFactorX, float &rFactorY ) const
379 // Get the position of the control
380 const Position *pPos = getPosition();
385 // Compute the resize factors
388 rFactorX = (float)pPos->getWidth() / (float)m_width;
392 rFactorY = (float)pPos->getHeight() / (float)m_height;