1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: ctrl_button.cpp,v 1.1 2004/01/03 23:31:33 asmax Exp $
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_button.hpp"
26 #include "../events/evt_generic.hpp"
27 #include "../src/generic_bitmap.hpp"
28 #include "../src/os_factory.hpp"
29 #include "../src/os_graphics.hpp"
30 #include "../commands/cmd_generic.hpp"
33 CtrlButton::CtrlButton( intf_thread_t *pIntf, const GenericBitmap &rBmpUp,
34 const GenericBitmap &rBmpOver,
35 const GenericBitmap &rBmpDown, CmdGeneric &rCommand,
36 const UString &rTooltip, const UString &rHelp ):
37 CtrlGeneric( pIntf, rHelp ), m_fsm( pIntf ), m_rCommand( rCommand ),
38 m_tooltip( rTooltip ),
39 m_cmdUpOverDownOver( this, &transUpOverDownOver ),
40 m_cmdDownOverUpOver( this, &transDownOverUpOver ),
41 m_cmdDownOverDown( this, &transDownOverDown ),
42 m_cmdDownDownOver( this, &transDownDownOver ),
43 m_cmdUpOverUp( this, &transUpOverUp ),
44 m_cmdUpUpOver( this, &transUpUpOver ),
45 m_cmdDownUp( this, &transDownUp ),
46 m_cmdUpHidden( this, &transUpHidden ),
47 m_cmdHiddenUp( this, &transHiddenUp )
49 // Build the images of the button
50 OSFactory *pOsFactory = OSFactory::instance( pIntf );
51 m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
53 m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
54 m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
55 rBmpDown.getHeight() );
56 m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
57 m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
58 rBmpOver.getHeight() );
59 m_pImgOver->drawBitmap( rBmpOver, 0, 0 );
62 m_fsm.addState( "up" );
63 m_fsm.addState( "down" );
64 m_fsm.addState( "upOver" );
65 m_fsm.addState( "downOver" );
66 m_fsm.addState( "hidden" );
69 m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
70 &m_cmdUpOverDownOver );
71 m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
72 &m_cmdDownOverUpOver );
73 m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
74 m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
75 m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
76 m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
77 m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
78 // XXX: It would be easy to use a "ANY" initial state to handle these
79 // four lines in only one. But till now it isn't worthwhile...
80 m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
81 m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
82 m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
83 m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
84 m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
87 m_fsm.setState( "up" );
92 CtrlButton::~CtrlButton()
94 SKINS_DELETE( m_pImgUp );
95 SKINS_DELETE( m_pImgDown );
96 SKINS_DELETE( m_pImgOver );
100 void CtrlButton::handleEvent( EvtGeneric &rEvent )
102 m_fsm.handleTransition( rEvent.getAsString() );
106 bool CtrlButton::mouseOver( int x, int y ) const
110 return m_pImg->hit( x, y );
119 void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
123 // Draw the current image
124 rImage.drawGraphics( *m_pImg, 0, 0, xDest, yDest );
129 void CtrlButton::transUpOverDownOver( SkinObject *pCtrl )
131 CtrlButton *pThis = (CtrlButton*)pCtrl;
132 pThis->captureMouse();
133 pThis->m_pImg = pThis->m_pImgDown;
134 pThis->notifyLayout();
138 void CtrlButton::transDownOverUpOver( SkinObject *pCtrl )
140 CtrlButton *pThis = (CtrlButton*)pCtrl;
141 pThis->releaseMouse();
142 pThis->m_pImg = pThis->m_pImgUp;
143 pThis->notifyLayout();
144 // Execute the command associated to this button
145 pThis->m_rCommand.execute();
149 void CtrlButton::transDownOverDown( SkinObject *pCtrl )
151 CtrlButton *pThis = (CtrlButton*)pCtrl;
152 pThis->m_pImg = pThis->m_pImgUp;
153 pThis->notifyLayout();
157 void CtrlButton::transDownDownOver( SkinObject *pCtrl )
159 CtrlButton *pThis = (CtrlButton*)pCtrl;
160 pThis->m_pImg = pThis->m_pImgDown;
161 pThis->notifyLayout();
165 void CtrlButton::transUpUpOver( SkinObject *pCtrl )
167 CtrlButton *pThis = (CtrlButton*)pCtrl;
168 pThis->m_pImg = pThis->m_pImgOver;
169 pThis->notifyLayout();
173 void CtrlButton::transUpOverUp( SkinObject *pCtrl )
175 CtrlButton *pThis = (CtrlButton*)pCtrl;
176 pThis->m_pImg = pThis->m_pImgUp;
177 pThis->notifyLayout();
181 void CtrlButton::transDownUp( SkinObject *pCtrl )
183 CtrlButton *pThis = (CtrlButton*)pCtrl;
184 pThis->releaseMouse();
188 void CtrlButton::transUpHidden( SkinObject *pCtrl )
190 CtrlButton *pThis = (CtrlButton*)pCtrl;
191 pThis->m_pImg = NULL;
192 pThis->notifyLayout();
196 void CtrlButton::transHiddenUp( SkinObject *pCtrl )
198 CtrlButton *pThis = (CtrlButton*)pCtrl;
199 pThis->m_pImg = pThis->m_pImgUp;
200 pThis->notifyLayout();