1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: var_text.cpp,v 1.2 2004/01/11 17:12:17 asmax Exp $
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 #include "var_text.hpp"
26 #include "../src/vlcproc.hpp"
27 #include "../src/var_manager.hpp"
28 #include "../vars/time.hpp"
29 #include "../vars/volume.hpp"
32 const string VarText::m_type = "text";
35 VarText::VarText( intf_thread_t *pIntf ): Variable( pIntf ),
36 m_text( pIntf, "" ), m_lastText( pIntf, "" )
43 // Remove the observers
44 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
45 pVlcProc->getTimeVar().delObserver( this );
46 pVlcProc->getVolumeVar().delObserver( this );
47 VarManager *pVarManager = VarManager::instance( getIntf() );
48 pVarManager->getHelpText().delObserver( this );
52 const UString VarText::get() const
55 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
57 // Fill a temporary UString object, and replace the escape characters
58 // ($H for help, $T for time, $V for volume)
59 UString temp( m_text );
61 while( (pos = temp.find( "$H" )) != UString::npos )
63 VarManager *pVarManager = VarManager::instance( getIntf() );
64 // We use .getRaw() to avoid replacing the $H recursively!
65 temp.replace( pos, 2, pVarManager->getHelpText().getRaw() );
67 while( (pos = temp.find( "$T" )) != UString::npos )
70 pVlcProc->getTimeVar().getAsStringTime().c_str() );
72 while( (pos = temp.find( "$V" )) != UString::npos )
75 pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
82 void VarText::set( const UString &rText )
84 // Avoid an infinite loop
90 // Stop observing other variables
91 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
92 pVlcProc->getTimeVar().delObserver( this );
93 pVlcProc->getVolumeVar().delObserver( this );
94 VarManager *pVarManager = VarManager::instance( getIntf() );
95 pVarManager->getHelpText().delObserver( this );
99 // Observe needed variables
100 if( m_text.find( "$H" ) != UString::npos )
102 pVarManager->getHelpText().addObserver( this );
104 if( m_text.find( "$T" ) != UString::npos )
106 pVlcProc->getTimeVar().addObserver( this );
108 if( m_text.find( "$V" ) != UString::npos )
110 pVlcProc->getVolumeVar().addObserver( this );
117 void VarText::onUpdate( Subject<VarPercent> &rVariable )
119 UString newText = get();
120 // If the text has changed, notify the observers
121 if( newText != m_lastText )
123 m_lastText = newText;
129 void VarText::onUpdate( Subject<VarText> &rVariable )
131 UString newText = get();
132 // If the text has changed, notify the observers
133 if( newText != m_lastText )
135 m_lastText = newText;