]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/controls/ctrl_button.cpp
skins2: fix RadialSlider (overflow)
[vlc] / modules / gui / skins2 / controls / ctrl_button.cpp
index 4b7c94a50165ca713ffa76c222eef18346b4c8a3..a1eb12559a6f0f576e57cebc775b68c91e100f43 100644 (file)
@@ -1,11 +1,11 @@
 /*****************************************************************************
  * ctrl_button.cpp
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
+ * Copyright (C) 2003 the VideoLAN team
  * $Id$
  *
  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
- *          Olivier Teulière <ipkiss@via.ecp.fr>
+ *          Olivier Teulière <ipkiss@via.ecp.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #include "ctrl_button.hpp"
 #include "../events/evt_generic.hpp"
 #include "../src/generic_bitmap.hpp"
+#include "../src/generic_layout.hpp"
 #include "../src/os_factory.hpp"
 #include "../src/os_graphics.hpp"
 #include "../commands/cmd_generic.hpp"
@@ -37,28 +38,12 @@ CtrlButton::CtrlButton( intf_thread_t *pIntf, const GenericBitmap &rBmpUp,
                         VarBool *pVisible ):
     CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
     m_rCommand( rCommand ), m_tooltip( rTooltip ),
-    m_cmdUpOverDownOver( this, &transUpOverDownOver ),
-    m_cmdDownOverUpOver( this, &transDownOverUpOver ),
-    m_cmdDownOverDown( this, &transDownOverDown ),
-    m_cmdDownDownOver( this, &transDownDownOver ),
-    m_cmdUpOverUp( this, &transUpOverUp ),
-    m_cmdUpUpOver( this, &transUpUpOver ),
-    m_cmdDownUp( this, &transDownUp ),
-    m_cmdUpHidden( this, &transUpHidden ),
-    m_cmdHiddenUp( this, &transHiddenUp )
-{
-    // Build the images of the button
-    OSFactory *pOsFactory = OSFactory::instance( pIntf );
-    m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
-                                             rBmpUp.getHeight() );
-    m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
-    m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
-                                               rBmpDown.getHeight() );
-    m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
-    m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
-                                               rBmpOver.getHeight() );
-    m_pImgOver->drawBitmap( rBmpOver, 0, 0 );
-
+    m_imgUp( pIntf, rBmpUp ), m_imgOver( pIntf, rBmpOver ),
+    m_imgDown( pIntf, rBmpDown ), m_pImg( NULL ), m_cmdUpOverDownOver( this ),
+    m_cmdDownOverUpOver( this ), m_cmdDownOverDown( this ),
+    m_cmdDownDownOver( this ), m_cmdUpOverUp( this ), m_cmdUpUpOver( this ),
+    m_cmdDownUp( this ), m_cmdUpHidden( this ), m_cmdHiddenUp( this )
+{
     // States
     m_fsm.addState( "up" );
     m_fsm.addState( "down" );
@@ -88,17 +73,27 @@ CtrlButton::CtrlButton( intf_thread_t *pIntf, const GenericBitmap &rBmpUp,
 
     // Initial state
     m_fsm.setState( "up" );
-    m_pImg = m_pImgUp;
+    setImage( &m_imgUp );
 }
 
 
 CtrlButton::~CtrlButton()
 {
-    SKINS_DELETE( m_pImgUp );
-    SKINS_DELETE( m_pImgDown );
-    SKINS_DELETE( m_pImgOver );
 }
 
+void CtrlButton::setLayout( GenericLayout *pLayout,
+                           const Position &rPosition )
+{
+    CtrlGeneric::setLayout( pLayout, rPosition );
+    m_pLayout->getActiveVar().addObserver( this );
+}
+
+
+void CtrlButton::unsetLayout()
+{
+    m_pLayout->getActiveVar().delObserver( this );
+    CtrlGeneric::unsetLayout();
+}
 
 void CtrlButton::handleEvent( EvtGeneric &rEvent )
 {
@@ -124,90 +119,108 @@ void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
     if( m_pImg )
     {
         // Draw the current image
-        rImage.drawGraphics( *m_pImg, 0, 0, xDest, yDest );
+        m_pImg->draw( rImage, xDest, yDest );
+    }
+}
+
+
+void CtrlButton::setImage( AnimBitmap *pImg )
+{
+    AnimBitmap *pOldImg = m_pImg;
+    m_pImg = pImg;
+
+    if( pOldImg )
+    {
+        pOldImg->stopAnim();
+        pOldImg->delObserver( this );
+    }
+
+    if( pImg )
+    {
+        pImg->startAnim();
+        pImg->addObserver( this );
     }
+
+    notifyLayoutMaxSize( pOldImg, pImg );
+}
+
+
+void CtrlButton::onUpdate( Subject<AnimBitmap> &rBitmap, void *arg )
+{
+    notifyLayout();
 }
 
 
-void CtrlButton::transUpOverDownOver( SkinObject *pCtrl )
+void CtrlButton::CmdUpOverDownOver::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    pThis->captureMouse();
-    const OSGraphics *pOldImg = pThis->m_pImg;
-    pThis->m_pImg = pThis->m_pImgDown;
-    pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
+    m_pParent->captureMouse();
+    m_pParent->setImage( &m_pParent->m_imgDown );
 }
 
 
-void CtrlButton::transDownOverUpOver( SkinObject *pCtrl )
+void CtrlButton::CmdDownOverUpOver::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    pThis->releaseMouse();
-    const OSGraphics *pOldImg = pThis->m_pImg;
-    pThis->m_pImg = pThis->m_pImgUp;
-    pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
+    m_pParent->releaseMouse();
+    m_pParent->setImage( &m_pParent->m_imgUp );
     // Execute the command associated to this button
-    pThis->m_rCommand.execute();
+    m_pParent->m_rCommand.execute();
 }
 
 
-void CtrlButton::transDownOverDown( SkinObject *pCtrl )
+void CtrlButton::CmdDownOverDown::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    const OSGraphics *pOldImg = pThis->m_pImg;
-    pThis->m_pImg = pThis->m_pImgUp;
-    pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
+    m_pParent->setImage( &m_pParent->m_imgUp );
 }
 
 
-void CtrlButton::transDownDownOver( SkinObject *pCtrl )
+void CtrlButton::CmdDownDownOver::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    const OSGraphics *pOldImg = pThis->m_pImg;
-    pThis->m_pImg = pThis->m_pImgDown;
-    pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
+    m_pParent->setImage( &m_pParent->m_imgDown );
 }
 
 
-void CtrlButton::transUpUpOver( SkinObject *pCtrl )
+void CtrlButton::CmdUpUpOver::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    const OSGraphics *pOldImg = pThis->m_pImg;
-    pThis->m_pImg = pThis->m_pImgOver;
-    pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
+    m_pParent->setImage( &m_pParent->m_imgOver );
 }
 
 
-void CtrlButton::transUpOverUp( SkinObject *pCtrl )
+void CtrlButton::CmdUpOverUp::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    const OSGraphics *pOldImg = pThis->m_pImg;
-    pThis->m_pImg = pThis->m_pImgUp;
-    pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
+    m_pParent->setImage( &m_pParent->m_imgUp );
 }
 
 
-void CtrlButton::transDownUp( SkinObject *pCtrl )
+void CtrlButton::CmdDownUp::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    pThis->releaseMouse();
+    m_pParent->releaseMouse();
 }
 
 
-void CtrlButton::transUpHidden( SkinObject *pCtrl )
+void CtrlButton::CmdUpHidden::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    const OSGraphics *pOldImg = pThis->m_pImg;
-    pThis->m_pImg = NULL;
-    pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
+    m_pParent->setImage( NULL );
 }
 
 
-void CtrlButton::transHiddenUp( SkinObject *pCtrl )
+void CtrlButton::CmdHiddenUp::execute()
 {
-    CtrlButton *pThis = (CtrlButton*)pCtrl;
-    const OSGraphics *pOldImg = pThis->m_pImg;
-    pThis->m_pImg = pThis->m_pImgUp;
-    pThis->notifyLayoutMaxSize( pOldImg, pThis->m_pImg );
+    m_pParent->setImage( &m_pParent->m_imgUp );
+}
+
+void CtrlButton::onUpdate( Subject<VarBool> &rVariable, void *arg  )
+{
+    // restart animation
+    if(     &rVariable == m_pVisible
+        ||  &rVariable == &m_pLayout->getActiveVar()
+      )
+    {
+        if( m_pImg )
+        {
+            m_pImg->stopAnim();
+            m_pImg->startAnim();
+        }
+    }
+    CtrlGeneric::onUpdate( rVariable, arg );
 }