]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/controls/ctrl_text.cpp
skins2: fix text control misfunctioning when changing skin
[vlc] / modules / gui / skins2 / controls / ctrl_text.cpp
index 2bc266994de3b978efe4ec55fa8c9072a69dd2f1..da48507ebce52fadd9b032a384417e522ea7c446 100644 (file)
@@ -6,6 +6,7 @@
  *
  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
  *          Olivier Teulière <ipkiss@via.ecp.fr>
+ *          Erwan Tulou      <erwan10 At videolan Dot org<
  *
  * 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
 
 CtrlText::CtrlText( intf_thread_t *pIntf, VarText &rVariable,
                     const GenericFont &rFont, const UString &rHelp,
-                    uint32_t color, VarBool *pVisible, Scrolling_t scrollMode,
-                    Align_t alignment ):
+                    uint32_t color, VarBool *pVisible, VarBool *pFocus,
+                    Scrolling_t scrollMode, Align_t alignment ):
     CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
     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_pFocus( pFocus), m_pImg( NULL ), m_pImgDouble( NULL ),
     m_pCurrImg( NULL ), m_xPos( 0 ), m_xOffset( 0 ),
     m_cmdUpdateText( this )
 {
@@ -91,34 +92,22 @@ CtrlText::CtrlText( intf_thread_t *pIntf, VarText &rVariable,
     }
 
     // Initial state
-    if( m_scrollMode == kAutomatic )
-        m_fsm.setState( "outMoving" );
-    else
-        m_fsm.setState( "outStill" );
+    m_fsm.setState( (m_scrollMode != kAutomatic) ? "outStill" : "outMoving" );
 
     // Observe the variable
     m_rVariable.addObserver( this );
 
-    // Set the text
-    displayText( m_rVariable.get() );
+    // initialize pictures
+    setPictures( m_rVariable.get() );
 }
 
 
 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;
 }
 
 
@@ -133,6 +122,9 @@ void CtrlText::handleEvent( EvtGeneric &rEvent )
 
 bool CtrlText::mouseOver( int x, int y ) const
 {
+    if( !m_pFocus->get() )
+        return false;
+
     if( m_pCurrImg )
     {
         // We have 3 different ways of deciding when to return true here:
@@ -162,8 +154,10 @@ bool CtrlText::mouseOver( int x, int y ) const
 }
 
 
-void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest )
+void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h )
 {
+    rect clip( xDest, yDest, w, h );
+    const Position *pPos = getPosition();
     if( m_pCurrImg )
     {
         // Compute the dimensions to draw
@@ -190,12 +184,18 @@ void CtrlText::draw( OSGraphics &rImage, int xDest, int yDest )
             else if( m_alignment == kCenter &&
                      width < getPosition()->getWidth() )
             {
-                    // The text is shorter than the width of the control, so we
-                    // can center it
+                // 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 );
+            rect region( pPos->getLeft() + offset,
+                         pPos->getTop(), width, height );
+            rect inter;
+            if( rect::intersect( region, clip, &inter ) )
+                rImage.drawBitmap( *m_pCurrImg, -m_xPos + inter.x - region.x,
+                                   inter.y - region.y,
+                                   inter.x, inter.y,
+                                   inter.width, inter.height, true );
         }
     }
 }
@@ -214,45 +214,91 @@ void CtrlText::setText( const UString &rText, uint32_t color )
 }
 
 
-void CtrlText::onUpdate( Subject<VarText, void*> &rVariable, void* arg )
+void CtrlText::onUpdate( Subject<VarText> &rVariable, void* arg )
 {
+    (void)rVariable; (void)arg;
     if( isVisible() )
     {
-        displayText( m_rVariable.get() );
+        setPictures( m_rVariable.get() );
+        updateContext();
+        notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
     }
 }
 
 
-void CtrlText::displayText( const UString &rText )
+void CtrlText::onUpdate( Subject<VarBool> &rVariable, void *arg  )
 {
-    // Create the images ('normal' and 'double') from the text
-    // 'Normal' image
-    if( m_pImg )
+    (void)arg;
+    // Visibility changed
+    if( &rVariable == m_pVisible )
     {
-        delete m_pImg;
+        if( isVisible() )
+        {
+            setPictures( m_rVariable.get() );
+            updateContext();
+            notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
+        }
     }
+}
+
+
+void CtrlText::setPictures( const UString &rText )
+{
+    // reset the images ('normal' and 'double') from the text
+    // 'Normal' image
+    delete m_pImg;
     m_pImg = m_rFont.drawString( rText, m_color );
     if( !m_pImg )
-    {
         return;
-    }
+
     // 'Double' image
     const UString doubleStringWithSep = rText + SEPARATOR_STRING + rText;
-    if( m_pImgDouble )
+    delete m_pImgDouble;
+    m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
+}
+
+
+void CtrlText::updateContext()
+{
+    if( !m_pImg || !getPosition() )
+        return;
+
+    if( m_pImg->getWidth() < getPosition()->getWidth() )
     {
-        delete m_pImgDouble;
+        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
+    {
+        m_pCurrImg = m_pImgDouble;
     }
-    m_pImgDouble = m_rFont.drawString( doubleStringWithSep, m_color );
 
-    // Update the current image used, as if the control size had changed
-    onChangePosition();
+    // If the control is in the moving state,
+    // automatically start or stop the timer accordingly
+    const string &rState = m_fsm.getState();
+    if( rState == "moving" || rState == "outMoving" )
+    {
+        if( m_pCurrImg == m_pImgDouble )
+        {
+            m_pTimer->start( MOVING_TEXT_DELAY, false );
+        }
+        else
+        {
+            m_pTimer->stop();
+        }
+    }
 
-    if( m_alignment == kRight && getPosition() &&
+    // compute alignment
+    if( m_alignment == kRight &&
         getPosition()->getWidth() < m_pImg->getWidth() )
     {
         m_xPos = getPosition()->getWidth() - m_pImg->getWidth();
     }
-    else if( m_alignment == kCenter && getPosition() &&
+    else if( m_alignment == kCenter &&
              getPosition()->getWidth() < m_pImg->getWidth() )
     {
         m_xPos = (getPosition()->getWidth() - m_pImg->getWidth()) / 2;
@@ -261,48 +307,18 @@ void CtrlText::displayText( const UString &rText )
     {
         m_xPos = 0;
     }
+}
 
-    if( getPosition() )
-    {
-        // If the control was in the moving state, check if the scrolling is
-        // still necessary
-        const string &rState = m_fsm.getState();
-        if( rState == "moving" || rState == "outMoving" )
-        {
-            if( m_pImg && m_pImg->getWidth() >= getPosition()->getWidth() )
-            {
-                m_pCurrImg = m_pImgDouble;
-                m_pTimer->start( MOVING_TEXT_DELAY, false );
-            }
-            else
-            {
-                m_pTimer->stop();
-            }
-        }
-        notifyLayout( getPosition()->getWidth(), getPosition()->getHeight() );
-    }
+
+void CtrlText::onPositionChange()
+{
+    updateContext();
 }
 
 
-void CtrlText::onChangePosition()
+void CtrlText::onResize()
 {
-    if( m_pImg && getPosition() )
-    {
-        if( m_pImg->getWidth() < getPosition()->getWidth() )
-        {
-            m_pCurrImg = m_pImg;
-        }
-        else
-        {
-            m_pCurrImg = m_pImgDouble;
-        }
-    }
-    else
-    {
-        // m_pImg is a better default value than m_pImgDouble, but anyway we
-        // don't care because the control is never drawn without position :)
-        m_pCurrImg = m_pImg;
-    }
+    updateContext();
 }
 
 
@@ -325,13 +341,9 @@ void CtrlText::CmdManualMoving::execute()
     // Start the automatic movement, but only if the text is wider than the
     // 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() )
+    if( m_pParent->m_pCurrImg &&
+        m_pParent->m_pCurrImg == m_pParent->m_pImgDouble )
     {
-        // The current image may have been set incorrectly in displayText(), so
-        // set the correct value
-        m_pParent->m_pCurrImg = m_pParent->m_pImgDouble;
-
         m_pParent->m_pTimer->start( MOVING_TEXT_DELAY, false );
     }
 }
@@ -347,14 +359,10 @@ void CtrlText::CmdMove::execute()
 {
     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
 
-    // Do nothing if the text fits in the control
-    if( m_pParent->m_pImg &&
-        m_pParent->m_pImg->getWidth() >= m_pParent->getPosition()->getWidth() )
+    // Move text only when it is larger than the control
+    if( m_pParent->m_pCurrImg &&
+        m_pParent->m_pCurrImg == m_pParent->m_pImgDouble )
     {
-        // The current image may have been set incorrectly in displayText(), so
-        // we set the correct value
-        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
         m_pParent->m_xPos = (pEvtMouse->getXPos() - m_pParent->m_xOffset);
@@ -378,13 +386,12 @@ void CtrlText::CmdUpdateText::execute()
 
 void CtrlText::adjust( int &position )
 {
+    if( !m_pImg || !m_pImgDouble )
+        return;
+
     // {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 )
-    {
-        return;
-    }
     position %= m_pImgDouble->getWidth() - m_pImg->getWidth();
     if( position > 0 )
     {