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 *****************************************************************************/
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,
38 CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
39 m_rCommand( rCommand ), m_tooltip( rTooltip ),
40 m_imgUp( pIntf, rBmpUp ), m_imgOver( pIntf, rBmpOver ),
41 m_imgDown( pIntf, rBmpDown ), m_pImg( NULL ), m_cmdUpOverDownOver( this ),
42 m_cmdDownOverUpOver( this ), m_cmdDownOverDown( this ),
43 m_cmdDownDownOver( this ), m_cmdUpOverUp( this ), m_cmdUpUpOver( this ),
44 m_cmdDownUp( this ), m_cmdUpHidden( this ), m_cmdHiddenUp( this )
47 m_fsm.addState( "up" );
48 m_fsm.addState( "down" );
49 m_fsm.addState( "upOver" );
50 m_fsm.addState( "downOver" );
51 m_fsm.addState( "hidden" );
54 m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
55 &m_cmdUpOverDownOver );
56 m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
57 &m_cmdUpOverDownOver );
58 m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
59 &m_cmdDownOverUpOver );
60 m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
61 m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
62 m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
63 m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
64 m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
65 // XXX: It would be easy to use a "ANY" initial state to handle these
66 // four lines in only one. But till now it isn't worthwhile...
67 m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
68 m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
69 m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
70 m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
71 m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
74 m_fsm.setState( "up" );
79 CtrlButton::~CtrlButton()
84 void CtrlButton::handleEvent( EvtGeneric &rEvent )
86 m_fsm.handleTransition( rEvent.getAsString() );
90 bool CtrlButton::mouseOver( int x, int y ) const
94 return m_pImg->hit( x, y );
103 void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
107 // Draw the current image
108 m_pImg->draw( rImage, xDest, yDest );
113 void CtrlButton::setImage( AnimBitmap *pImg )
115 AnimBitmap *pOldImg = m_pImg;
121 pOldImg->delObserver( this );
127 pImg->addObserver( this );
130 notifyLayoutMaxSize( pOldImg, pImg );
134 void CtrlButton::onUpdate( Subject<AnimBitmap, void*> &rBitmap, void *arg )
140 void CtrlButton::CmdUpOverDownOver::execute()
142 m_pParent->captureMouse();
143 m_pParent->setImage( &m_pParent->m_imgDown );
147 void CtrlButton::CmdDownOverUpOver::execute()
149 m_pParent->releaseMouse();
150 m_pParent->setImage( &m_pParent->m_imgUp );
151 // Execute the command associated to this button
152 m_pParent->m_rCommand.execute();
156 void CtrlButton::CmdDownOverDown::execute()
158 m_pParent->setImage( &m_pParent->m_imgUp );
162 void CtrlButton::CmdDownDownOver::execute()
164 m_pParent->setImage( &m_pParent->m_imgDown );
168 void CtrlButton::CmdUpUpOver::execute()
170 m_pParent->setImage( &m_pParent->m_imgOver );
174 void CtrlButton::CmdUpOverUp::execute()
176 m_pParent->setImage( &m_pParent->m_imgUp );
180 void CtrlButton::CmdDownUp::execute()
182 m_pParent->releaseMouse();
186 void CtrlButton::CmdUpHidden::execute()
188 m_pParent->setImage( NULL );
192 void CtrlButton::CmdHiddenUp::execute()
194 m_pParent->setImage( &m_pParent->m_imgUp );