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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * ctrl_checkbox.cpp
  3.  *****************************************************************************
  4.  * Copyright (C) 2003 VideoLAN
  5.  * $Id: ctrl_checkbox.cpp 8079 2004-06-27 19:27:01Z gbazin $
  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_checkbox.hpp"
  25. #include "../events/evt_generic.hpp"
  26. #include "../commands/cmd_generic.hpp"
  27. #include "../src/generic_bitmap.hpp"
  28. #include "../src/os_factory.hpp"
  29. #include "../src/os_graphics.hpp"
  30. #include "../utils/var_bool.hpp"
  31. CtrlCheckbox::CtrlCheckbox( intf_thread_t *pIntf,
  32.                             const GenericBitmap &rBmpUp1,
  33.                             const GenericBitmap &rBmpOver1,
  34.                             const GenericBitmap &rBmpDown1,
  35.                             const GenericBitmap &rBmpUp2,
  36.                             const GenericBitmap &rBmpOver2,
  37.                             const GenericBitmap &rBmpDown2,
  38.                             CmdGeneric &rCommand1, CmdGeneric &rCommand2,
  39.                             const UString &rTooltip1,
  40.                             const UString &rTooltip2,
  41.                             VarBool &rVariable, const UString &rHelp,
  42.                             VarBool *pVisible ):
  43.     CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
  44.     m_rVariable( rVariable ),
  45.     m_rCommand1( rCommand1 ), m_rCommand2( rCommand2 ),
  46.     m_tooltip1( rTooltip1 ), m_tooltip2( rTooltip2 ),
  47.     m_cmdUpOverDownOver( this, &transUpOverDownOver ),
  48.     m_cmdDownOverUpOver( this, &transDownOverUpOver ),
  49.     m_cmdDownOverDown( this, &transDownOverDown ),
  50.     m_cmdDownDownOver( this, &transDownDownOver ),
  51.     m_cmdUpOverUp( this, &transUpOverUp ),
  52.     m_cmdUpUpOver( this, &transUpUpOver ),
  53.     m_cmdDownUp( this, &transDownUp ),
  54.     m_cmdUpHidden( this, &transUpHidden ),
  55.     m_cmdHiddenUp( this, &transHiddenUp )
  56. {
  57.     // Build the images of the checkbox
  58.     OSFactory *pOsFactory = OSFactory::instance( pIntf );
  59.     m_pImgUp1 = pOsFactory->createOSGraphics( rBmpUp1.getWidth(),
  60.                                               rBmpUp1.getHeight() );
  61.     m_pImgUp1->drawBitmap( rBmpUp1, 0, 0 );
  62.     m_pImgDown1 = pOsFactory->createOSGraphics( rBmpDown1.getWidth(),
  63.                                                 rBmpDown1.getHeight() );
  64.     m_pImgDown1->drawBitmap( rBmpDown1, 0, 0 );
  65.     m_pImgOver1 = pOsFactory->createOSGraphics( rBmpOver1.getWidth(),
  66.                                                 rBmpOver1.getHeight() );
  67.     m_pImgOver1->drawBitmap( rBmpOver1, 0, 0 );
  68.     m_pImgUp2 = pOsFactory->createOSGraphics( rBmpUp2.getWidth(),
  69.                                               rBmpUp2.getHeight() );
  70.     m_pImgUp2->drawBitmap( rBmpUp2, 0, 0 );
  71.     m_pImgDown2 = pOsFactory->createOSGraphics( rBmpDown2.getWidth(),
  72.                                                 rBmpDown2.getHeight() );
  73.     m_pImgDown2->drawBitmap( rBmpDown2, 0, 0 );
  74.     m_pImgOver2 = pOsFactory->createOSGraphics( rBmpOver2.getWidth(),
  75.                                                 rBmpOver2.getHeight() );
  76.     m_pImgOver2->drawBitmap( rBmpOver2, 0, 0 );
  77.     // States
  78.     m_fsm.addState( "up" );
  79.     m_fsm.addState( "down" );
  80.     m_fsm.addState( "upOver" );
  81.     m_fsm.addState( "downOver" );
  82.     m_fsm.addState( "hidden" );
  83.     // Transitions
  84.     m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
  85.                          &m_cmdUpOverDownOver );
  86.     m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
  87.                          &m_cmdUpOverDownOver );
  88.     m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
  89.                          &m_cmdDownOverUpOver );
  90.     m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
  91.     m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
  92.     m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
  93.     m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
  94.     m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
  95.     // XXX: It would be easy to use a "ANY" initial state to handle these
  96.     // four lines in only one. But till now it isn't worthwhile...
  97.     m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
  98.     m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
  99.     m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
  100.     m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
  101.     m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
  102.     // Observe the variable
  103.     m_rVariable.addObserver( this );
  104.     // Initial state
  105.     m_fsm.setState( "up" );
  106.     if( !m_rVariable.get() )
  107.     {
  108.         m_pImgUp = m_pImgUp1;
  109.         m_pImgOver = m_pImgOver1;
  110.         m_pImgDown = m_pImgDown1;
  111.         m_pImgCurrent = m_pImgUp;
  112.         m_pCommand = &m_rCommand1;
  113.         m_pTooltip = &m_tooltip1;
  114.     }
  115.     else
  116.     {
  117.         m_pImgUp = m_pImgUp2;
  118.         m_pImgOver = m_pImgOver2;
  119.         m_pImgDown = m_pImgDown2;
  120.         m_pImgCurrent = m_pImgDown;
  121.         m_pCommand = &m_rCommand2;
  122.         m_pTooltip = &m_tooltip2;
  123.     }
  124. }
  125. CtrlCheckbox::~CtrlCheckbox()
  126. {
  127.     m_rVariable.delObserver( this );
  128.     SKINS_DELETE( m_pImgUp1 );
  129.     SKINS_DELETE( m_pImgDown1 );
  130.     SKINS_DELETE( m_pImgOver1 );
  131.     SKINS_DELETE( m_pImgUp2 );
  132.     SKINS_DELETE( m_pImgDown2 );
  133.     SKINS_DELETE( m_pImgOver2 );
  134. }
  135. void CtrlCheckbox::handleEvent( EvtGeneric &rEvent )
  136. {
  137.     m_fsm.handleTransition( rEvent.getAsString() );
  138. }
  139. bool CtrlCheckbox::mouseOver( int x, int y ) const
  140. {
  141.     if( m_pImgCurrent )
  142.     {
  143.         return m_pImgCurrent->hit( x, y );
  144.     }
  145.     else
  146.     {
  147.         return false;
  148.     }
  149. }
  150. void CtrlCheckbox::draw( OSGraphics &rImage, int xDest, int yDest )
  151. {
  152.     if( m_pImgCurrent )
  153.     {
  154.         // Draw the current image
  155.         rImage.drawGraphics( *m_pImgCurrent, 0, 0, xDest, yDest );
  156.     }
  157. }
  158. void CtrlCheckbox::transUpOverDownOver( SkinObject *pCtrl )
  159. {
  160.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  161.     pThis->captureMouse();
  162.     pThis->m_pImgCurrent = pThis->m_pImgDown;
  163.     pThis->notifyLayout();
  164. }
  165. void CtrlCheckbox::transDownOverUpOver( SkinObject *pCtrl )
  166. {
  167.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  168.     pThis->releaseMouse();
  169.     // Invert the state variable
  170.     pThis->m_pImgCurrent = pThis->m_pImgUp;
  171.     pThis->notifyLayout();
  172.     // Execute the command
  173.     pThis->m_pCommand->execute();
  174. }
  175. void CtrlCheckbox::transDownOverDown( SkinObject *pCtrl )
  176. {
  177.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  178.     pThis->m_pImgCurrent = pThis->m_pImgUp;
  179.     pThis->notifyLayout();
  180. }
  181. void CtrlCheckbox::transDownDownOver( SkinObject *pCtrl )
  182. {
  183.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  184.     pThis->m_pImgCurrent = pThis->m_pImgDown;
  185.     pThis->notifyLayout();
  186. }
  187. void CtrlCheckbox::transUpUpOver( SkinObject *pCtrl )
  188. {
  189.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  190.     pThis->m_pImgCurrent = pThis->m_pImgOver;
  191.     pThis->notifyLayout();
  192. }
  193. void CtrlCheckbox::transUpOverUp( SkinObject *pCtrl )
  194. {
  195.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  196.     pThis->m_pImgCurrent = pThis->m_pImgUp;
  197.     pThis->notifyLayout();
  198. }
  199. void CtrlCheckbox::transDownUp( SkinObject *pCtrl )
  200. {
  201.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  202.     pThis->releaseMouse();
  203. }
  204. void CtrlCheckbox::transUpHidden( SkinObject *pCtrl )
  205. {
  206.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  207.     pThis->m_pImgCurrent = NULL;
  208.     pThis->notifyLayout();
  209. }
  210. void CtrlCheckbox::transHiddenUp( SkinObject *pCtrl )
  211. {
  212.     CtrlCheckbox *pThis = (CtrlCheckbox*)pCtrl;
  213.     pThis->m_pImgCurrent = pThis->m_pImgUp;
  214.     pThis->notifyLayout();
  215. }
  216. void CtrlCheckbox::onVarBoolUpdate( VarBool &rVariable )
  217. {
  218.     changeButton();
  219. }
  220. void CtrlCheckbox::changeButton()
  221. {
  222.     // Are we using the first set of images or the second one?
  223.     if( m_pImgUp == m_pImgUp1 )
  224.     {
  225.         m_pImgUp = m_pImgUp2;
  226.         m_pImgOver = m_pImgOver2;
  227.         m_pImgDown = m_pImgDown2;
  228.         m_pTooltip = &m_tooltip2;
  229.         m_pCommand = &m_rCommand2;
  230.     }
  231.     else
  232.     {
  233.         m_pImgUp = m_pImgUp1;
  234.         m_pImgOver = m_pImgOver1;
  235.         m_pImgDown = m_pImgDown1;
  236.         m_pTooltip = &m_tooltip1;
  237.         m_pCommand = &m_rCommand1;
  238.     }
  239.     // XXX: We assume that the checkbox is up
  240.     m_pImgCurrent = m_pImgUp;
  241.     // Notify the window the tooltip has changed
  242.     notifyTooltipChange();
  243.     // Refresh
  244.     notifyLayout();
  245. }