Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

ctrl_checkbox.cpp

00001 /*****************************************************************************
00002  * ctrl_checkbox.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: ctrl_checkbox.cpp 12207 2005-08-15 15:54:32Z asmax $
00006  *
00007  * Authors: Cyril Deguet     <[email protected]>
00008  *          Olivier Teulière <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 #include "ctrl_checkbox.hpp"
00026 #include "../events/evt_generic.hpp"
00027 #include "../commands/cmd_generic.hpp"
00028 #include "../src/generic_bitmap.hpp"
00029 #include "../src/os_factory.hpp"
00030 #include "../src/os_graphics.hpp"
00031 #include "../utils/var_bool.hpp"
00032 
00033 
00034 CtrlCheckbox::CtrlCheckbox( intf_thread_t *pIntf,
00035                             const GenericBitmap &rBmpUp1,
00036                             const GenericBitmap &rBmpOver1,
00037                             const GenericBitmap &rBmpDown1,
00038                             const GenericBitmap &rBmpUp2,
00039                             const GenericBitmap &rBmpOver2,
00040                             const GenericBitmap &rBmpDown2,
00041                             CmdGeneric &rCommand1, CmdGeneric &rCommand2,
00042                             const UString &rTooltip1,
00043                             const UString &rTooltip2,
00044                             VarBool &rVariable, const UString &rHelp,
00045                             VarBool *pVisible ):
00046     CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
00047     m_rVariable( rVariable ),
00048     m_rCommand1( rCommand1 ), m_rCommand2( rCommand2 ),
00049     m_tooltip1( rTooltip1 ), m_tooltip2( rTooltip2 ),
00050     m_cmdUpOverDownOver( this ), m_cmdDownOverUpOver( this ),
00051     m_cmdDownOverDown( this ), m_cmdDownDownOver( this ),
00052     m_cmdUpOverUp( this ), m_cmdUpUpOver( this ),
00053     m_cmdDownUp( this ), m_cmdUpHidden( this ),
00054     m_cmdHiddenUp( this )
00055 {
00056     // Build the images of the checkbox
00057     OSFactory *pOsFactory = OSFactory::instance( pIntf );
00058     m_pImgUp1 = pOsFactory->createOSGraphics( rBmpUp1.getWidth(),
00059                                               rBmpUp1.getHeight() );
00060     m_pImgUp1->drawBitmap( rBmpUp1, 0, 0 );
00061     m_pImgDown1 = pOsFactory->createOSGraphics( rBmpDown1.getWidth(),
00062                                                 rBmpDown1.getHeight() );
00063     m_pImgDown1->drawBitmap( rBmpDown1, 0, 0 );
00064     m_pImgOver1 = pOsFactory->createOSGraphics( rBmpOver1.getWidth(),
00065                                                 rBmpOver1.getHeight() );
00066     m_pImgOver1->drawBitmap( rBmpOver1, 0, 0 );
00067 
00068     m_pImgUp2 = pOsFactory->createOSGraphics( rBmpUp2.getWidth(),
00069                                               rBmpUp2.getHeight() );
00070     m_pImgUp2->drawBitmap( rBmpUp2, 0, 0 );
00071     m_pImgDown2 = pOsFactory->createOSGraphics( rBmpDown2.getWidth(),
00072                                                 rBmpDown2.getHeight() );
00073     m_pImgDown2->drawBitmap( rBmpDown2, 0, 0 );
00074     m_pImgOver2 = pOsFactory->createOSGraphics( rBmpOver2.getWidth(),
00075                                                 rBmpOver2.getHeight() );
00076     m_pImgOver2->drawBitmap( rBmpOver2, 0, 0 );
00077 
00078     // States
00079     m_fsm.addState( "up" );
00080     m_fsm.addState( "down" );
00081     m_fsm.addState( "upOver" );
00082     m_fsm.addState( "downOver" );
00083     m_fsm.addState( "hidden" );
00084 
00085     // Transitions
00086     m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
00087                          &m_cmdUpOverDownOver );
00088     m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
00089                          &m_cmdUpOverDownOver );
00090     m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
00091                          &m_cmdDownOverUpOver );
00092     m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
00093     m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
00094     m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
00095     m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
00096     m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
00097     // XXX: It would be easy to use a "ANY" initial state to handle these
00098     // four lines in only one. But till now it isn't worthwhile...
00099     m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
00100     m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
00101     m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
00102     m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
00103     m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
00104 
00105     // Observe the variable
00106     m_rVariable.addObserver( this );
00107 
00108     // Initial state
00109     m_fsm.setState( "up" );
00110     if( !m_rVariable.get() )
00111     {
00112         m_pImgUp = m_pImgUp1;
00113         m_pImgOver = m_pImgOver1;
00114         m_pImgDown = m_pImgDown1;
00115         m_pImgCurrent = m_pImgUp;
00116         m_pCommand = &m_rCommand1;
00117         m_pTooltip = &m_tooltip1;
00118     }
00119     else
00120     {
00121         m_pImgUp = m_pImgUp2;
00122         m_pImgOver = m_pImgOver2;
00123         m_pImgDown = m_pImgDown2;
00124         m_pImgCurrent = m_pImgDown;
00125         m_pCommand = &m_rCommand2;
00126         m_pTooltip = &m_tooltip2;
00127     }
00128 }
00129 
00130 
00131 CtrlCheckbox::~CtrlCheckbox()
00132 {
00133     m_rVariable.delObserver( this );
00134     SKINS_DELETE( m_pImgUp1 );
00135     SKINS_DELETE( m_pImgDown1 );
00136     SKINS_DELETE( m_pImgOver1 );
00137     SKINS_DELETE( m_pImgUp2 );
00138     SKINS_DELETE( m_pImgDown2 );
00139     SKINS_DELETE( m_pImgOver2 );
00140 }
00141 
00142 
00143 void CtrlCheckbox::handleEvent( EvtGeneric &rEvent )
00144 {
00145     m_fsm.handleTransition( rEvent.getAsString() );
00146 }
00147 
00148 
00149 bool CtrlCheckbox::mouseOver( int x, int y ) const
00150 {
00151     if( m_pImgCurrent )
00152     {
00153         return m_pImgCurrent->hit( x, y );
00154     }
00155     else
00156     {
00157         return false;
00158     }
00159 }
00160 
00161 
00162 void CtrlCheckbox::draw( OSGraphics &rImage, int xDest, int yDest )
00163 {
00164     if( m_pImgCurrent )
00165     {
00166         // Draw the current image
00167         rImage.drawGraphics( *m_pImgCurrent, 0, 0, xDest, yDest );
00168     }
00169 }
00170 
00171 
00172 void CtrlCheckbox::CmdUpOverDownOver::execute()
00173 {
00174     m_pParent->captureMouse();
00175     const OSGraphics *pOldImg = m_pParent->m_pImgCurrent;
00176     m_pParent->m_pImgCurrent = m_pParent->m_pImgDown;
00177     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImgCurrent );
00178 }
00179 
00180 
00181 void CtrlCheckbox::CmdDownOverUpOver::execute()
00182 {
00183     m_pParent->releaseMouse();
00184 
00185     // Invert the state variable
00186     const OSGraphics *pOldImg = m_pParent->m_pImgCurrent;
00187     m_pParent->m_pImgCurrent = m_pParent->m_pImgUp;
00188     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImgCurrent );
00189 
00190     // Execute the command
00191     m_pParent->m_pCommand->execute();
00192 }
00193 
00194 
00195 void CtrlCheckbox::CmdDownOverDown::execute()
00196 {
00197     const OSGraphics *pOldImg = m_pParent->m_pImgCurrent;
00198     m_pParent->m_pImgCurrent = m_pParent->m_pImgUp;
00199     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImgCurrent );
00200 }
00201 
00202 
00203 void CtrlCheckbox::CmdDownDownOver::execute()
00204 {
00205     const OSGraphics *pOldImg = m_pParent->m_pImgCurrent;
00206     m_pParent->m_pImgCurrent = m_pParent->m_pImgDown;
00207     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImgCurrent );
00208 }
00209 
00210 
00211 void CtrlCheckbox::CmdUpUpOver::execute()
00212 {
00213     const OSGraphics *pOldImg = m_pParent->m_pImgCurrent;
00214     m_pParent->m_pImgCurrent = m_pParent->m_pImgOver;
00215     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImgCurrent );
00216 }
00217 
00218 
00219 void CtrlCheckbox::CmdUpOverUp::execute()
00220 {
00221     const OSGraphics *pOldImg = m_pParent->m_pImgCurrent;
00222     m_pParent->m_pImgCurrent = m_pParent->m_pImgUp;
00223     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImgCurrent );
00224 }
00225 
00226 
00227 void CtrlCheckbox::CmdDownUp::execute()
00228 {
00229     m_pParent->releaseMouse();
00230 }
00231 
00232 
00233 void CtrlCheckbox::CmdUpHidden::execute()
00234 {
00235     const OSGraphics *pOldImg = m_pParent->m_pImgCurrent;
00236     m_pParent->m_pImgCurrent = NULL;
00237     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImgCurrent );
00238 }
00239 
00240 
00241 void CtrlCheckbox::CmdHiddenUp::execute()
00242 {
00243     const OSGraphics *pOldImg = m_pParent->m_pImgCurrent;
00244     m_pParent->m_pImgCurrent = m_pParent->m_pImgUp;
00245     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImgCurrent );
00246 }
00247 
00248 
00249 void CtrlCheckbox::onVarBoolUpdate( VarBool &rVariable )
00250 {
00251     changeButton();
00252 }
00253 
00254 
00255 void CtrlCheckbox::changeButton()
00256 {
00257     // Are we using the first set of images or the second one?
00258     if( m_pImgUp == m_pImgUp1 )
00259     {
00260         m_pImgUp = m_pImgUp2;
00261         m_pImgOver = m_pImgOver2;
00262         m_pImgDown = m_pImgDown2;
00263         m_pTooltip = &m_tooltip2;
00264         m_pCommand = &m_rCommand2;
00265     }
00266     else
00267     {
00268         m_pImgUp = m_pImgUp1;
00269         m_pImgOver = m_pImgOver1;
00270         m_pImgDown = m_pImgDown1;
00271         m_pTooltip = &m_tooltip1;
00272         m_pCommand = &m_rCommand1;
00273     }
00274     // XXX: We assume that the checkbox is up
00275     m_pImgCurrent = m_pImgUp;
00276 
00277     // Notify the window the tooltip has changed
00278     notifyTooltipChange();
00279     // Refresh
00280     notifyLayout();
00281 }

Generated on Tue Dec 20 10:14:41 2005 for vlc-0.8.4a by  doxygen 1.4.2