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_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, &transUpOverDownOver ),
41 m_cmdDownOverUpOver( this, &transDownOverUpOver ),
42 m_cmdDownOverDown( this, &transDownOverDown ),
43 m_cmdDownDownOver( this, &transDownDownOver ),
44 m_cmdUpOverUp( this, &transUpOverUp ),
45 m_cmdUpUpOver( this, &transUpUpOver ),
46 m_cmdDownUp( this, &transDownUp ),
47 m_cmdUpHidden( this, &transUpHidden ),
48 m_cmdHiddenUp( this, &transHiddenUp )
50 // Build the images of the button
51 OSFactory *pOsFactory = OSFactory::instance( pIntf );
52 m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
54 m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
55 m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
56 rBmpDown.getHeight() );
57 m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
58 m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
59 rBmpOver.getHeight() );
60 m_pImgOver->drawBitmap( rBmpOver, 0, 0 );
63 m_fsm.addState( "up" );
64 m_fsm.addState( "down" );
65 m_fsm.addState( "upOver" );
66 m_fsm.addState( "downOver" );
67 m_fsm.addState( "hidden" );
70 m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
71 &m_cmdUpOverDownOver );
72 m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
73 &m_cmdUpOverDownOver );
74 m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
75 &m_cmdDownOverUpOver );
76 m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
77 m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
78 m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
79 m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
80 m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
81 // XXX: It would be easy to use a "ANY" initial state to handle these
82 // four lines in only one. But till now it isn't worthwhile...
83 m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
84 m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
85 m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
86 m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
87 m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
90 m_fsm.setState( "up" );
95 CtrlButton::~CtrlButton()
97 SKINS_DELETE( m_pImgUp );
98 SKINS_DELETE( m_pImgDown );
99 SKINS_DELETE( m_pImgOver );
103 void CtrlButton::handleEvent( EvtGeneric &rEvent )
105 m_fsm.handleTransition( rEvent.getAsString() );
109 bool CtrlButton::mouseOver( int x, int y ) const
113 return m_pImg->hit( x, y );
122 void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
126 // Draw the current image
127 rImage.drawGraphics( *m_pImg, 0, 0, xDest, yDest );
132 void CtrlButton::transUpOverDownOver( SkinObject *pCtrl )
134 CtrlButton *pThis = (CtrlButton*)pCtrl;
135 pThis->captureMouse();
136 const OSGraphics *pOldImg = pThis->m_pImg;
137 pThis->m_pImg = pThis->m_pImgDown;
138 pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
142 void CtrlButton::transDownOverUpOver( SkinObject *pCtrl )
144 CtrlButton *pThis = (CtrlButton*)pCtrl;
145 pThis->releaseMouse();
146 const OSGraphics *pOldImg = pThis->m_pImg;
147 pThis->m_pImg = pThis->m_pImgUp;
148 pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
149 // Execute the command associated to this button
150 pThis->m_rCommand.execute();
154 void CtrlButton::transDownOverDown( SkinObject *pCtrl )
156 CtrlButton *pThis = (CtrlButton*)pCtrl;
157 const OSGraphics *pOldImg = pThis->m_pImg;
158 pThis->m_pImg = pThis->m_pImgUp;
159 pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
163 void CtrlButton::transDownDownOver( SkinObject *pCtrl )
165 CtrlButton *pThis = (CtrlButton*)pCtrl;
166 const OSGraphics *pOldImg = pThis->m_pImg;
167 pThis->m_pImg = pThis->m_pImgDown;
168 pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
172 void CtrlButton::transUpUpOver( SkinObject *pCtrl )
174 CtrlButton *pThis = (CtrlButton*)pCtrl;
175 const OSGraphics *pOldImg = pThis->m_pImg;
176 pThis->m_pImg = pThis->m_pImgOver;
177 pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
181 void CtrlButton::transUpOverUp( SkinObject *pCtrl )
183 CtrlButton *pThis = (CtrlButton*)pCtrl;
184 const OSGraphics *pOldImg = pThis->m_pImg;
185 pThis->m_pImg = pThis->m_pImgUp;
186 pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
190 void CtrlButton::transDownUp( SkinObject *pCtrl )
192 CtrlButton *pThis = (CtrlButton*)pCtrl;
193 pThis->releaseMouse();
197 void CtrlButton::transUpHidden( SkinObject *pCtrl )
199 CtrlButton *pThis = (CtrlButton*)pCtrl;
200 const OSGraphics *pOldImg = pThis->m_pImg;
201 pThis->m_pImg = NULL;
202 pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
206 void CtrlButton::transHiddenUp( SkinObject *pCtrl )
208 CtrlButton *pThis = (CtrlButton*)pCtrl;
209 const OSGraphics *pOldImg = pThis->m_pImg;
210 pThis->m_pImg = pThis->m_pImgUp;
211 pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );