]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/controls/ctrl_text.cpp
skins2: optimize refresh of text control
[vlc] / modules / gui / skins2 / controls / ctrl_text.cpp
old mode 100755 (executable)
new mode 100644 (file)
index b03db58..867c661
@@ -1,11 +1,11 @@
 /*****************************************************************************
  * ctrl_text.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
@@ -19,7 +19,7 @@
  *
  * 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_text.hpp"
 
 CtrlText::CtrlText( intf_thread_t *pIntf, VarText &rVariable,
                     const GenericFont &rFont, const UString &rHelp,
-                    uint32_t color, VarBool *pVisible ):
+                    uint32_t color, VarBool *pVisible, Scrolling_t scrollMode,
+                    Align_t alignment ):
     CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
-    m_rVariable( rVariable ), m_cmdToManual( this, &transToManual ),
-    m_cmdManualMoving( this, &transManualMoving ),
-    m_cmdManualStill( this, &transManualStill ),
-    m_cmdMove( this, &transMove ),
-    m_pEvt( NULL ), m_rFont( rFont ), m_color( color ),
-    m_pImg( NULL ), m_pImgDouble( NULL ), m_pCurrImg( NULL ),
-    m_xPos( 0 ), m_xOffset( 0 )
+    m_rVariable( rVariable ), m_cmdToManual( this ),
+    m_cmdManualMoving( this ), m_cmdManualStill( this ),
+    m_cmdMove( this ), m_pEvt( NULL ), m_rFont( rFont ),
+    m_color( color ), m_scrollMode( scrollMode ), m_alignment( alignment ),
+    m_pImg( NULL ), m_pImgDouble( NULL ),
+    m_pCurrImg( NULL ), m_xPos( 0 ), m_xOffset( 0 ),
+    m_cmdUpdateText( this )
 {
-    m_pTimer = OSFactory::instance( getIntf() )->createOSTimer(
-        Callback( this, &updateText ) );
+    m_pTimer = OSFactory::instance( pIntf )->createOSTimer( m_cmdUpdateText );
 
     // States
     m_fsm.addState( "still" );
@@ -64,23 +64,34 @@ CtrlText::CtrlText( intf_thread_t *pIntf, VarText &rVariable,
     m_fsm.addState( "outMoving" );
 
     // Transitions
-    m_fsm.addTransition( "still", "mouse:left:down", "manual1",
-                         &m_cmdToManual );
-    m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
-                         &m_cmdManualMoving );
-    m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
-                         &m_cmdToManual );
-    m_fsm.addTransition( "manual2", "mouse:left:up", "still",
-                         &m_cmdManualStill );
-    m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
-    m_fsm.addTransition( "manual2", "motion", "manual2", &m_cmdMove );
     m_fsm.addTransition( "still", "leave", "outStill" );
     m_fsm.addTransition( "outStill", "enter", "still" );
-    m_fsm.addTransition( "moving", "leave", "outMoving" );
-    m_fsm.addTransition( "outMoving", "enter", "moving" );
+    if( m_scrollMode == kManual )
+    {
+        m_fsm.addTransition( "still", "mouse:left:down", "manual1",
+                             &m_cmdToManual );
+        m_fsm.addTransition( "manual1", "mouse:left:up", "still",
+                             &m_cmdManualStill );
+        m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
+    }
+    else if( m_scrollMode == kAutomatic )
+    {
+        m_fsm.addTransition( "still", "mouse:left:down", "manual1",
+                             &m_cmdToManual );
+        m_fsm.addTransition( "manual1", "mouse:left:up", "moving",
+                             &m_cmdManualMoving );
+        m_fsm.addTransition( "moving", "mouse:left:down", "manual2",
+                             &m_cmdToManual );
+        m_fsm.addTransition( "manual2", "mouse:left:up", "still",
+                             &m_cmdManualStill );
+        m_fsm.addTransition( "manual1", "motion", "manual1", &m_cmdMove );
+        m_fsm.addTransition( "manual2", "motion", "manual2", &m_cmdMove );
+        m_fsm.addTransition( "moving", "leave", "outMoving" );
+        m_fsm.addTransition( "outMoving", "enter", "moving" );
+    }
 
     // Initial state
-    m_fsm.setState( "moving" );
+    m_fsm.setState( "outStill" );
 
     // Observe the variable
     m_rVariable.addObserver( this );
@@ -93,18 +104,9 @@ CtrlText::CtrlText( intf_thread_t *pIntf, VarText &rVariable,
 CtrlText::~CtrlText()
 {
     m_rVariable.delObserver( this );
-    if( m_pTimer )
-    {
-        delete m_pTimer;
-    }
-    if( m_pImg )
-    {
-        delete m_pImg;
-    }
-    if( m_pImgDouble )
-    {
-        delete m_pImgDouble;
-    }
+    delete m_pTimer;
+    delete m_pImg;
+    delete m_pImgDouble;
 }
 
 
@@ -159,8 +161,29 @@ void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest )
         // Draw the current image
         if( width > 0 && height > 0 )
         {
-            rImage.drawBitmap( *m_pCurrImg, -m_xPos, 0, xDest, yDest,
-                            width, height );
+            int offset = 0;
+            if( m_alignment == kLeft )
+            {
+                // We align to the left
+                offset = 0;
+            }
+            else if( m_alignment == kRight &&
+                     width < getPosition()->getWidth() )
+
+            {
+                // The text is shorter than the width of the control, so we
+                // can align it to the right
+                offset = getPosition()->getWidth() - width;
+            }
+            else if( m_alignment == kCenter &&
+                     width < getPosition()->getWidth() )
+            {
+                    // The text is shorter than the width of the control, so we
+                    // can center it
+                offset = (getPosition()->getWidth() - width) / 2;
+            }
+            rImage.drawBitmap( *m_pCurrImg, -m_xPos, 0, xDest + offset,
+                               yDest, width, height, true );
         }
     }
 }
@@ -179,9 +202,31 @@ void CtrlText::setText( const UString &rText, uint32_t color )
 }
 
 
-void CtrlText::onUpdate( Subject<VarText> &rVariable )
+void CtrlText::onUpdate( Subject<VarText> &rVariable, void* arg )
 {
-    displayText( m_rVariable.get() );
+    if( isVisible() )
+    {
+        displayText( m_rVariable.get() );
+        notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
+    }
+}
+
+
+void CtrlText::onUpdate( Subject<VarBool> &rVariable, void *arg  )
+{
+    // Visibility changed
+    if( &rVariable == m_pVisible )
+    {
+        if( isVisible() )
+        {
+            displayText( m_rVariable.get() );
+            notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
+        }
+        else
+        {
+            notifyLayout();
+        }
+    }
 }
 
 
@@ -189,10 +234,7 @@ void CtrlText::displayText( const UString &rText )
 {
     // Create the images ('normal' and 'double') from the text
     // 'Normal' image
-    if( m_pImg )
-    {
-        delete m_pImg;
-    }
+    delete m_pImg;
     m_pImg = m_rFont.drawString( rText, m_color );
     if( !m_pImg )
     {
@@ -200,15 +242,26 @@ void CtrlText::displayText( const UString &rText )
     }
     // 'Double' image
     const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
-    if( m_pImgDouble )
-    {
-        delete m_pImgDouble;
-    }
+    delete m_pImgDouble;
     m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
 
     // Update the current image used, as if the control size had changed
-    onChangePosition();
-    m_xPos = 0;
+    onPositionChange();
+
+    if( m_alignment == kRight && getPosition() &&
+        getPosition()->getWidth() < m_pImg->getWidth() )
+    {
+        m_xPos = getPosition()->getWidth() - m_pImg->getWidth();
+    }
+    else if( m_alignment == kCenter && getPosition() &&
+             getPosition()->getWidth() < m_pImg->getWidth() )
+    {
+        m_xPos = (getPosition()->getWidth() - m_pImg->getWidth()) / 2;
+    }
+    else
+    {
+        m_xPos = 0;
+    }
 
     if( getPosition() )
     {
@@ -227,18 +280,22 @@ void CtrlText::displayText( const UString &rText )
                 m_pTimer->stop();
             }
         }
-        notifyLayout();
     }
 }
 
 
-void CtrlText::onChangePosition()
+void CtrlText::onPositionChange()
 {
     if( m_pImg && getPosition() )
     {
         if( m_pImg->getWidth() < getPosition()->getWidth() )
         {
             m_pCurrImg = m_pImg;
+
+            // When the control becomes wide enough for the text to display,
+            // make sure to stop any scrolling effect
+            m_pTimer->stop();
+            m_xPos = 0;
         }
         else
         {
@@ -254,82 +311,85 @@ void CtrlText::onChangePosition()
 }
 
 
-void CtrlText::transToManual( SkinObject *pCtrl )
+void CtrlText::onResize()
+{
+    onPositionChange();
+}
+
+
+void CtrlText::CmdToManual::execute()
 {
-    CtrlText *pThis = (CtrlText*)pCtrl;
-    EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
+    EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
 
     // Compute the offset
-    pThis->m_xOffset = pEvtMouse->getXPos() - pThis->m_xPos;
+    m_pParent->m_xOffset = pEvtMouse->getXPos() - m_pParent->m_xPos;
 
-    pThis->m_pTimer->stop();
-    pThis->captureMouse();
+    m_pParent->m_pTimer->stop();
+    m_pParent->captureMouse();
 }
 
 
-void CtrlText::transManualMoving( SkinObject *pCtrl )
+void CtrlText::CmdManualMoving::execute()
 {
-    CtrlText *pThis = (CtrlText*)pCtrl;
-    pThis->releaseMouse();
+    m_pParent->releaseMouse();
 
     // Start the automatic movement, but only if the text is wider than the
-    // control
-    if( pThis->m_pImg &&
-        pThis->m_pImg->getWidth() >= pThis->getPosition()->getWidth() )
+    // control and if the control can scroll (either in manual or automatic
+    // mode)
+    if( m_pParent->m_pImg &&
+        m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
     {
         // The current image may have been set incorrectly in displayText(), so
         // set the correct value
-        pThis->m_pCurrImg = pThis->m_pImgDouble;
+        m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
 
-        pThis->m_pTimer->start( MOVING_TEXT_DELAY, false );
+        m_pParent->m_pTimer->start( MOVING_TEXT_DELAY, false );
     }
 }
 
 
-void CtrlText::transManualStill( SkinObject *pCtrl )
+void CtrlText::CmdManualStill::execute()
 {
-    CtrlText *pThis = (CtrlText*)pCtrl;
-    pThis->releaseMouse();
+    m_pParent->releaseMouse();
 }
 
 
-void CtrlText::transMove( SkinObject *pCtrl )
+void CtrlText::CmdMove::execute()
 {
-    CtrlText *pThis = (CtrlText*)pCtrl;
-    EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
+    EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
 
     // Do nothing if the text fits in the control
-    if( pThis->m_pImg &&
-        pThis->m_pImg->getWidth() >= pThis->getPosition()->getWidth() )
+    if( m_pParent->m_pImg &&
+        m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
     {
         // The current image may have been set incorrectly in displayText(), so
         // we set the correct value
-        pThis->m_pCurrImg = pThis->m_pImgDouble;
+        m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
 
         // Compute the new position of the left side, and make sure it is
         // in the correct range
-        pThis->m_xPos = (pEvtMouse->getXPos() - pThis->m_xOffset);
-        pThis->adjust( pThis->m_xPos );
+        m_pParent->m_xPos = (pEvtMouse->getXPos() - m_pParent->m_xOffset);
+        m_pParent->adjust( m_pParent->m_xPos );
 
-        pThis->notifyLayout();
+        m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
+                                 m_pParent->getPosition()->getHeight() );
     }
 }
 
 
-void CtrlText::updateText( SkinObject *pCtrl )
+void CtrlText::CmdUpdateText::execute()
 {
-    CtrlText *pThis = (CtrlText*)pCtrl;
-
-    pThis->m_xPos -= MOVING_TEXT_STEP;
-    pThis->adjust( pThis->m_xPos );
+    m_pParent->m_xPos -= MOVING_TEXT_STEP;
+    m_pParent->adjust( m_pParent->m_xPos );
 
-    pThis->notifyLayout();
+    m_pParent->notifyLayout( m_pParent->getPosition()->getWidth(),
+                             m_pParent->getPosition()->getHeight() );
 }
 
 
 void CtrlText::adjust( int &position )
 {
-    // {m_pImgDouble->getWidth()  - m_pImg->getWidth()} is the period of the
+    // {m_pImgDouble->getWidth() - m_pImg->getWidth()} is the period of the
     // bitmap; remember that the string used to generate m_pImgDouble is of the
     // form: "foo  foo", the number of spaces being a parameter
     if( !m_pImg )