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/generic_layout.hpp"
29 #include "../src/os_factory.hpp"
30 #include "../src/os_graphics.hpp"
31 #include "../commands/cmd_generic.hpp"
34 CtrlButton::CtrlButton( intf_thread_t *pIntf, const GenericBitmap &rBmpUp,
35 const GenericBitmap &rBmpOver,
36 const GenericBitmap &rBmpDown, CmdGeneric &rCommand,
37 const UString &rTooltip, const UString &rHelp,
39 CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
40 m_rCommand( rCommand ), m_tooltip( rTooltip ),
41 m_imgUp( pIntf, rBmpUp ), m_imgOver( pIntf, rBmpOver ),
42 m_imgDown( pIntf, rBmpDown ), m_pImg( NULL ), m_cmdUpOverDownOver( this ),
43 m_cmdDownOverUpOver( this ), m_cmdDownOverDown( this ),
44 m_cmdDownDownOver( this ), m_cmdUpOverUp( this ), m_cmdUpUpOver( this ),
45 m_cmdDownUp( this ), m_cmdUpHidden( this ), m_cmdHiddenUp( this )
48 m_fsm.addState( "up" );
49 m_fsm.addState( "down" );
50 m_fsm.addState( "upOver" );
51 m_fsm.addState( "downOver" );
52 m_fsm.addState( "hidden" );
55 m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
56 &m_cmdUpOverDownOver );
57 m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
58 &m_cmdUpOverDownOver );
59 m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
60 &m_cmdDownOverUpOver );
61 m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
62 m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
63 m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
64 m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
65 m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
66 // XXX: It would be easy to use a "ANY" initial state to handle these
67 // four lines in only one. But till now it isn't worthwhile...
68 m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
69 m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
70 m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
71 m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
72 m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
75 m_fsm.setState( "up" );
80 CtrlButton::~CtrlButton()
84 void CtrlButton::setLayout( GenericLayout *pLayout,
85 const Position &rPosition )
87 CtrlGeneric::setLayout( pLayout, rPosition );
88 m_pLayout->getActiveVar().addObserver( this );
92 void CtrlButton::unsetLayout()
94 m_pLayout->getActiveVar().delObserver( this );
95 CtrlGeneric::unsetLayout();
98 void CtrlButton::handleEvent( EvtGeneric &rEvent )
100 m_fsm.handleTransition( rEvent.getAsString() );
104 bool CtrlButton::mouseOver( int x, int y ) const
108 return m_pImg->hit( x, y );
117 void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
121 // Draw the current image
122 m_pImg->draw( rImage, xDest, yDest );
127 void CtrlButton::setImage( AnimBitmap *pImg )
129 AnimBitmap *pOldImg = m_pImg;
135 pOldImg->delObserver( this );
141 pImg->addObserver( this );
144 notifyLayoutMaxSize( pOldImg, pImg );
148 void CtrlButton::onUpdate( Subject<AnimBitmap> &rBitmap, void *arg )
154 void CtrlButton::CmdUpOverDownOver::execute()
156 m_pParent->captureMouse();
157 m_pParent->setImage( &m_pParent->m_imgDown );
161 void CtrlButton::CmdDownOverUpOver::execute()
163 m_pParent->releaseMouse();
164 m_pParent->setImage( &m_pParent->m_imgUp );
165 // Execute the command associated to this button
166 m_pParent->m_rCommand.execute();
170 void CtrlButton::CmdDownOverDown::execute()
172 m_pParent->setImage( &m_pParent->m_imgUp );
176 void CtrlButton::CmdDownDownOver::execute()
178 m_pParent->setImage( &m_pParent->m_imgDown );
182 void CtrlButton::CmdUpUpOver::execute()
184 m_pParent->setImage( &m_pParent->m_imgOver );
188 void CtrlButton::CmdUpOverUp::execute()
190 m_pParent->setImage( &m_pParent->m_imgUp );
194 void CtrlButton::CmdDownUp::execute()
196 m_pParent->releaseMouse();
200 void CtrlButton::CmdUpHidden::execute()
202 m_pParent->setImage( NULL );
206 void CtrlButton::CmdHiddenUp::execute()
208 m_pParent->setImage( &m_pParent->m_imgUp );
211 void CtrlButton::onUpdate( Subject<VarBool> &rVariable, void *arg )
214 if( &rVariable == m_pVisible
215 || &rVariable == &m_pLayout->getActiveVar()
224 CtrlGeneric::onUpdate( rVariable, arg );