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., 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,
38 CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
39 m_rCommand( rCommand ), m_tooltip( rTooltip ),
40 m_cmdUpOverDownOver( this ), m_cmdDownOverUpOver( this ),
41 m_cmdDownOverDown( this ), m_cmdDownDownOver( this ),
42 m_cmdUpOverUp( this ), m_cmdUpUpOver( this ),
43 m_cmdDownUp( this ), m_cmdUpHidden( this ),
46 // Build the images of the button
47 OSFactory *pOsFactory = OSFactory::instance( pIntf );
48 m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
50 m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
51 m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
52 rBmpDown.getHeight() );
53 m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
54 m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
55 rBmpOver.getHeight() );
56 m_pImgOver->drawBitmap( rBmpOver, 0, 0 );
59 m_fsm.addState( "up" );
60 m_fsm.addState( "down" );
61 m_fsm.addState( "upOver" );
62 m_fsm.addState( "downOver" );
63 m_fsm.addState( "hidden" );
66 m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
67 &m_cmdUpOverDownOver );
68 m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
69 &m_cmdUpOverDownOver );
70 m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
71 &m_cmdDownOverUpOver );
72 m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
73 m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
74 m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
75 m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
76 m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
77 // XXX: It would be easy to use a "ANY" initial state to handle these
78 // four lines in only one. But till now it isn't worthwhile...
79 m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
80 m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
81 m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
82 m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
83 m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
86 m_fsm.setState( "up" );
91 CtrlButton::~CtrlButton()
93 SKINS_DELETE( m_pImgUp );
94 SKINS_DELETE( m_pImgDown );
95 SKINS_DELETE( m_pImgOver );
99 void CtrlButton::handleEvent( EvtGeneric &rEvent )
101 m_fsm.handleTransition( rEvent.getAsString() );
105 bool CtrlButton::mouseOver( int x, int y ) const
109 return m_pImg->hit( x, y );
118 void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
122 // Draw the current image
123 rImage.drawGraphics( *m_pImg, 0, 0, xDest, yDest );
128 void CtrlButton::CmdUpOverDownOver::execute()
130 m_pParent->captureMouse();
131 const OSGraphics *pOldImg = m_pParent->m_pImg;
132 m_pParent->m_pImg = m_pParent->m_pImgDown;
133 m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
137 void CtrlButton::CmdDownOverUpOver::execute()
139 m_pParent->releaseMouse();
140 const OSGraphics *pOldImg = m_pParent->m_pImg;
141 m_pParent->m_pImg = m_pParent->m_pImgUp;
142 m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
143 // Execute the command associated to this button
144 m_pParent->m_rCommand.execute();
148 void CtrlButton::CmdDownOverDown::execute()
150 const OSGraphics *pOldImg = m_pParent->m_pImg;
151 m_pParent->m_pImg = m_pParent->m_pImgUp;
152 m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
156 void CtrlButton::CmdDownDownOver::execute()
158 const OSGraphics *pOldImg = m_pParent->m_pImg;
159 m_pParent->m_pImg = m_pParent->m_pImgDown;
160 m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
164 void CtrlButton::CmdUpUpOver::execute()
166 const OSGraphics *pOldImg = m_pParent->m_pImg;
167 m_pParent->m_pImg = m_pParent->m_pImgOver;
168 m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
172 void CtrlButton::CmdUpOverUp::execute()
174 const OSGraphics *pOldImg = m_pParent->m_pImg;
175 m_pParent->m_pImg = m_pParent->m_pImgUp;
176 m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
180 void CtrlButton::CmdDownUp::execute()
182 m_pParent->releaseMouse();
186 void CtrlButton::CmdUpHidden::execute()
188 const OSGraphics *pOldImg = m_pParent->m_pImg;
189 m_pParent->m_pImg = NULL;
190 m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
194 void CtrlButton::CmdHiddenUp::execute()
196 const OSGraphics *pOldImg = m_pParent->m_pImg;
197 m_pParent->m_pImg = m_pParent->m_pImgUp;
198 m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );