]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/var_text.cpp
* modules/gui/skins/*:
[vlc] / modules / gui / skins2 / utils / var_text.cpp
1 /*****************************************************************************
2  * var_text.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: var_text.cpp,v 1.2 2004/01/11 17:12:17 asmax Exp $
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
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"
30
31
32 const string VarText::m_type = "text";
33
34
35 VarText::VarText( intf_thread_t *pIntf ): Variable( pIntf ),
36     m_text( pIntf, "" ), m_lastText( pIntf, "" )
37 {
38 }
39
40
41 VarText::~VarText()
42 {
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 );
49 }
50
51
52 const UString VarText::get() const
53 {
54     uint32_t pos;
55     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
56
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 );
60
61     while( (pos = temp.find( "$H" )) != UString::npos )
62     {
63         VarManager *pVarManager = VarManager::instance( getIntf() );
64         // We use .getRaw() to avoid replacing the $H recursively!
65         temp.replace( pos, 2, pVarManager->getHelpText().getRaw() );
66     }
67     while( (pos = temp.find( "$T" )) != UString::npos )
68     {
69         temp.replace( pos, 2,
70                       pVlcProc->getTimeVar().getAsStringTime().c_str() );
71     }
72     while( (pos = temp.find( "$V" )) != UString::npos )
73     {
74         temp.replace( pos, 2,
75                       pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
76     }
77
78     return temp;
79 }
80
81
82 void VarText::set( const UString &rText )
83 {
84     // Avoid an infinite loop
85     if( rText == m_text )
86     {
87         return;
88     }
89
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 );
96
97     m_text = rText;
98
99     // Observe needed variables
100     if( m_text.find( "$H" ) != UString::npos )
101     {
102         pVarManager->getHelpText().addObserver( this );
103     }
104     if( m_text.find( "$T" ) != UString::npos )
105     {
106         pVlcProc->getTimeVar().addObserver( this );
107     }
108     if( m_text.find( "$V" ) != UString::npos )
109     {
110         pVlcProc->getVolumeVar().addObserver( this );
111     }
112
113     notify();
114 }
115
116
117 void VarText::onUpdate( Subject<VarPercent> &rVariable )
118 {
119     UString newText = get();
120     // If the text has changed, notify the observers
121     if( newText != m_lastText )
122     {
123         m_lastText = newText;
124         notify();
125     }
126 }
127
128
129 void VarText::onUpdate( Subject<VarText> &rVariable )
130 {
131     UString newText = get();
132     // If the text has changed, notify the observers
133     if( newText != m_lastText )
134     {
135         m_lastText = newText;
136         notify();
137     }
138 }