]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/var_text.cpp
dd16ad01a7470c1ade1fb27ec168d2099702b983
[vlc] / modules / gui / skins2 / utils / var_text.cpp
1 /*****************************************************************************
2  * var_text.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
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 #include "../vars/stream.hpp"
31
32
33 const string VarText::m_type = "text";
34
35
36 VarText::VarText( intf_thread_t *pIntf ): Variable( pIntf ),
37     m_text( pIntf, "" ), m_lastText( pIntf, "" )
38 {
39 }
40
41
42 VarText::~VarText()
43 {
44     // Remove the observers
45     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
46     pVlcProc->getTimeVar().delObserver( this );
47     pVlcProc->getVolumeVar().delObserver( this );
48     pVlcProc->getStreamVar().delObserver( this );
49     VarManager *pVarManager = VarManager::instance( getIntf() );
50     pVarManager->getHelpText().delObserver( this );
51 }
52
53
54 const UString VarText::get() const
55 {
56     uint32_t pos;
57     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
58
59     // Fill a temporary UString object, and replace the escape characters
60     // ($H for help, $T for current time, $L for time left, $D for duration,
61     // $V for volume)
62     UString temp( m_text );
63
64     while( (pos = temp.find( "$H" )) != UString::npos )
65     {
66         VarManager *pVarManager = VarManager::instance( getIntf() );
67         // We use .getRaw() to avoid replacing the $H recursively!
68         temp.replace( pos, 2, pVarManager->getHelpText().getRaw() );
69     }
70     while( (pos = temp.find( "$T" )) != UString::npos )
71     {
72         temp.replace( pos, 2,
73                       pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
74     }
75     while( (pos = temp.find( "$L" )) != UString::npos )
76     {
77         temp.replace( pos, 2,
78                       pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
79     }
80     while( (pos = temp.find( "$D" )) != UString::npos )
81     {
82         temp.replace( pos, 2,
83                       pVlcProc->getTimeVar().getAsStringDuration().c_str() );
84     }
85     while( (pos = temp.find( "$V" )) != UString::npos )
86     {
87         temp.replace( pos, 2,
88                       pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
89     }
90     while( (pos = temp.find( "$N" )) != UString::npos )
91     {
92         temp.replace( pos, 2,
93                       pVlcProc->getStreamVar().getAsStringName().c_str() );
94     }
95     while( (pos = temp.find( "$F" )) != UString::npos )
96     {
97         temp.replace( pos, 2,
98                       pVlcProc->getStreamVar().getAsStringFullName().c_str() );
99     }
100
101     return temp;
102 }
103
104
105 void VarText::set( const UString &rText )
106 {
107     // Avoid an infinite loop
108     if( rText == m_text )
109     {
110         return;
111     }
112
113     // Stop observing other variables
114     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
115     pVlcProc->getTimeVar().delObserver( this );
116     pVlcProc->getVolumeVar().delObserver( this );
117     pVlcProc->getStreamVar().delObserver( this );
118     VarManager *pVarManager = VarManager::instance( getIntf() );
119     pVarManager->getHelpText().delObserver( this );
120
121     m_text = rText;
122
123     // Observe needed variables
124     if( m_text.find( "$H" ) != UString::npos )
125     {
126         pVarManager->getHelpText().addObserver( this );
127     }
128     if( m_text.find( "$T" ) != UString::npos )
129     {
130         pVlcProc->getTimeVar().addObserver( this );
131     }
132     if( m_text.find( "$L" ) != UString::npos )
133     {
134         pVlcProc->getTimeVar().addObserver( this );
135     }
136     if( m_text.find( "$D" ) != UString::npos )
137     {
138         pVlcProc->getTimeVar().addObserver( this );
139     }
140     if( m_text.find( "$V" ) != UString::npos )
141     {
142         pVlcProc->getVolumeVar().addObserver( this );
143     }
144     if( m_text.find( "$N" ) != UString::npos )
145     {
146         pVlcProc->getStreamVar().addObserver( this );
147     }
148     if( m_text.find( "$F" ) != UString::npos )
149     {
150         pVlcProc->getStreamVar().addObserver( this );
151     }
152
153     notify();
154 }
155
156
157 void VarText::onUpdate( Subject<VarPercent> &rVariable )
158 {
159     UString newText = get();
160     // If the text has changed, notify the observers
161     if( newText != m_lastText )
162     {
163         m_lastText = newText;
164         notify();
165     }
166 }
167
168
169 void VarText::onUpdate( Subject<VarText> &rVariable )
170 {
171     UString newText = get();
172     // If the text has changed, notify the observers
173     if( newText != m_lastText )
174     {
175         m_lastText = newText;
176         notify();
177     }
178 }