ctrl_button.cpp
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:6k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * ctrl_button.cpp
  3.  *****************************************************************************
  4.  * Copyright (C) 2003 VideoLAN
  5.  * $Id: ctrl_button.cpp 8066 2004-06-25 22:47:56Z asmax $
  6.  *
  7.  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
  8.  *          Olivier Teuli鑢e <ipkiss@via.ecp.fr>
  9.  *
  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.
  14.  *
  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.
  19.  *
  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.  *****************************************************************************/
  24. #include "ctrl_button.hpp"
  25. #include "../events/evt_generic.hpp"
  26. #include "../src/generic_bitmap.hpp"
  27. #include "../src/os_factory.hpp"
  28. #include "../src/os_graphics.hpp"
  29. #include "../commands/cmd_generic.hpp"
  30. CtrlButton::CtrlButton( intf_thread_t *pIntf, const GenericBitmap &rBmpUp,
  31.                         const GenericBitmap &rBmpOver,
  32.                         const GenericBitmap &rBmpDown, CmdGeneric &rCommand,
  33.                         const UString &rTooltip, const UString &rHelp,
  34.                         VarBool *pVisible ):
  35.     CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
  36.     m_rCommand( rCommand ), m_tooltip( rTooltip ),
  37.     m_cmdUpOverDownOver( this, &transUpOverDownOver ),
  38.     m_cmdDownOverUpOver( this, &transDownOverUpOver ),
  39.     m_cmdDownOverDown( this, &transDownOverDown ),
  40.     m_cmdDownDownOver( this, &transDownDownOver ),
  41.     m_cmdUpOverUp( this, &transUpOverUp ),
  42.     m_cmdUpUpOver( this, &transUpUpOver ),
  43.     m_cmdDownUp( this, &transDownUp ),
  44.     m_cmdUpHidden( this, &transUpHidden ),
  45.     m_cmdHiddenUp( this, &transHiddenUp )
  46. {
  47.     // Build the images of the button
  48.     OSFactory *pOsFactory = OSFactory::instance( pIntf );
  49.     m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
  50.                                              rBmpUp.getHeight() );
  51.     m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
  52.     m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
  53.                                                rBmpDown.getHeight() );
  54.     m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
  55.     m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
  56.                                                rBmpOver.getHeight() );
  57.     m_pImgOver->drawBitmap( rBmpOver, 0, 0 );
  58.     // States
  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" );
  64.     // Transitions
  65.     m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
  66.                          &m_cmdUpOverDownOver );
  67.     m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
  68.                          &m_cmdUpOverDownOver );
  69.     m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
  70.                          &m_cmdDownOverUpOver );
  71.     m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
  72.     m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
  73.     m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
  74.     m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
  75.     m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
  76.     // XXX: It would be easy to use a "ANY" initial state to handle these
  77.     // four lines in only one. But till now it isn't worthwhile...
  78.     m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
  79.     m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
  80.     m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
  81.     m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
  82.     m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
  83.     // Initial state
  84.     m_fsm.setState( "up" );
  85.     m_pImg = m_pImgUp;
  86. }
  87. CtrlButton::~CtrlButton()
  88. {
  89.     SKINS_DELETE( m_pImgUp );
  90.     SKINS_DELETE( m_pImgDown );
  91.     SKINS_DELETE( m_pImgOver );
  92. }
  93. void CtrlButton::handleEvent( EvtGeneric &rEvent )
  94. {
  95.     m_fsm.handleTransition( rEvent.getAsString() );
  96. }
  97. bool CtrlButton::mouseOver( int x, int y ) const
  98. {
  99.     if( m_pImg )
  100.     {
  101.         return m_pImg->hit( x, y );
  102.     }
  103.     else
  104.     {
  105.         return false;
  106.     }
  107. }
  108. void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
  109. {
  110.     if( m_pImg )
  111.     {
  112.         // Draw the current image
  113.         rImage.drawGraphics( *m_pImg, 0, 0, xDest, yDest );
  114.     }
  115. }
  116. void CtrlButton::transUpOverDownOver( SkinObject *pCtrl )
  117. {
  118.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  119.     pThis->captureMouse();
  120.     pThis->m_pImg = pThis->m_pImgDown;
  121.     pThis->notifyLayout();
  122. }
  123. void CtrlButton::transDownOverUpOver( SkinObject *pCtrl )
  124. {
  125.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  126.     pThis->releaseMouse();
  127.     pThis->m_pImg = pThis->m_pImgUp;
  128.     pThis->notifyLayout();
  129.     // Execute the command associated to this button
  130.     pThis->m_rCommand.execute();
  131. }
  132. void CtrlButton::transDownOverDown( SkinObject *pCtrl )
  133. {
  134.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  135.     pThis->m_pImg = pThis->m_pImgUp;
  136.     pThis->notifyLayout();
  137. }
  138. void CtrlButton::transDownDownOver( SkinObject *pCtrl )
  139. {
  140.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  141.     pThis->m_pImg = pThis->m_pImgDown;
  142.     pThis->notifyLayout();
  143. }
  144. void CtrlButton::transUpUpOver( SkinObject *pCtrl )
  145. {
  146.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  147.     pThis->m_pImg = pThis->m_pImgOver;
  148.     pThis->notifyLayout();
  149. }
  150. void CtrlButton::transUpOverUp( SkinObject *pCtrl )
  151. {
  152.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  153.     pThis->m_pImg = pThis->m_pImgUp;
  154.     pThis->notifyLayout();
  155. }
  156. void CtrlButton::transDownUp( SkinObject *pCtrl )
  157. {
  158.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  159.     pThis->releaseMouse();
  160. }
  161. void CtrlButton::transUpHidden( SkinObject *pCtrl )
  162. {
  163.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  164.     pThis->m_pImg = NULL;
  165.     pThis->notifyLayout();
  166. }
  167. void CtrlButton::transHiddenUp( SkinObject *pCtrl )
  168. {
  169.     CtrlButton *pThis = (CtrlButton*)pCtrl;
  170.     pThis->m_pImg = pThis->m_pImgUp;
  171.     pThis->notifyLayout();
  172. }