]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
3ad7171e475c882f730d92d3ca138f771d136c8a
[vlc] / modules / gui / skins2 / parser / skin_parser.cpp
1 /*****************************************************************************
2  * skin_parser.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "skin_parser.hpp"
25 #include <math.h>
26
27 // Current DTD version
28 #define SKINS_DTD_VERSION "2.0"
29
30
31 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName ):
32     XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 )
33 {
34 }
35
36
37 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
38 {
39     if( rName == "Anchor" )
40     {
41         const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
42                 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
43                 atoi( attr["priority"] ), attr["points"], m_curWindowId );
44         m_data.m_listAnchor.push_back( anchor );
45     }
46
47     else if( rName == "Bitmap" )
48     {
49         const BuilderData::Bitmap bitmap( attr["id"] , attr["file"],
50                 ConvertColor( attr["alphacolor"] ) );
51         m_data.m_listBitmap.push_back( bitmap );
52     }
53
54     else if( rName == "Button" )
55     {
56         const BuilderData::Button button( uniqueId( attr["id"] ), atoi( attr["x"] ) +
57                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
58                 attr["rightbottom"], attr["visible"], attr["up"], attr["down"],
59                 attr["over"], attr["action"], attr["tooltiptext"], attr["help"],
60                 m_curLayer, m_curWindowId, m_curLayoutId );
61         m_curLayer++;
62         m_data.m_listButton.push_back( button );
63     }
64
65     else if( rName == "Checkbox" )
66     {
67         const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ), atoi( attr["x"] ) +
68                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
69                 attr["rightbottom"], attr["visible"], attr["up1"], attr["down1"], attr["over1"],
70                 attr["up2"], attr["down2"], attr["over2"], attr["state"],
71                 attr["action1"], attr["action2"], attr["tooltiptext1"],
72                 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
73                 m_curLayoutId );
74         m_curLayer++;
75         m_data.m_listCheckbox.push_back( checkbox );
76     }
77
78     else if( rName == "Font" )
79     {
80         const BuilderData::Font fontData( attr["id"], attr["file"],
81                 atoi( attr["size"] ) );
82         m_data.m_listFont.push_back( fontData );
83     }
84
85     else if( rName == "Group" )
86     {
87         m_xOffset += atoi( attr["x"] );
88         m_yOffset += atoi( attr["y"] );
89         m_xOffsetList.push_back( atoi( attr["x"] ) );
90         m_yOffsetList.push_back( atoi( attr["y"] ) );
91     }
92
93     else if( rName == "Image" )
94     {
95         const BuilderData::Image imageData( uniqueId( attr["id"] ), atoi( attr["x"] ) +
96                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
97                 attr["rightbottom"], attr["visible"],
98                 attr["image"], attr["action"], attr["help"], m_curLayer,
99                 m_curWindowId, m_curLayoutId );
100         m_curLayer++;
101         m_data.m_listImage.push_back( imageData );
102     }
103
104     else if( rName == "Layout" )
105     {
106         m_curLayoutId = uniqueId( attr["id"] );
107         const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
108                 atoi( attr["height"] ), atoi( attr["minwidth"] ),
109                 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
110                 atoi( attr["maxheight"] ), m_curWindowId );
111         m_data.m_listLayout.push_back( layout );
112         m_curLayer = 0;
113     }
114
115     else if( rName == "Playlist" )
116     {
117         m_curListId = uniqueId( attr["id"] );
118         const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
119                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
120                 atoi( attr["width"]), atoi( attr["height"] ),
121                 attr["lefttop"], attr["rightbottom"],
122                 attr["font"], attr["var"], ConvertColor( attr["fgcolor"] ),
123                 ConvertColor( attr["playcolor"] ),
124                 ConvertColor( attr["bgcolor1"] ),
125                 ConvertColor( attr["bgcolor2"] ),
126                 ConvertColor( attr["selcolor"] ), attr["help"],
127                 m_curLayer, m_curWindowId, m_curLayoutId );
128         m_curLayer++;
129         m_data.m_listList.push_back( listData );
130     }
131
132     else if( rName == "RadialSlider" )
133     {
134         const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
135                 attr["visible"],
136                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
137                 attr["lefttop"], attr["rightbottom"], attr["sequence"],
138                 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI / 180,
139                 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
140                 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
141                 m_curLayoutId );
142         m_curLayer++;
143         m_data.m_listRadialSlider.push_back( radial );
144     }
145
146     else if( rName == "Slider" )
147     {
148         string newValue = attr["value"];
149         if( m_curListId != "" )
150         {
151             // Slider associated to a list
152             newValue = "playlist.slider";
153         }
154         const BuilderData::Slider slider( uniqueId( attr["id"] ),
155                 attr["visible"],
156                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
157                 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
158                 attr["over"], attr["points"], atoi( attr["thickness"] ),
159                 newValue, attr["tooltiptext"], attr["help"], m_curLayer,
160                 m_curWindowId, m_curLayoutId );
161         m_curLayer++;
162         m_data.m_listSlider.push_back( slider );
163     }
164
165     else if( rName == "Text" )
166     {
167         const BuilderData::Text textData( uniqueId( attr["id"] ),
168                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
169                 attr["visible"], attr["font"],
170                 attr["text"], atoi( attr["width"] ),
171                 ConvertColor( attr["color"] ), attr["help"], m_curLayer,
172                 m_curWindowId, m_curLayoutId );
173         m_curLayer++;
174         m_data.m_listText.push_back( textData );
175     }
176
177     else if( rName == "Theme" )
178     {
179         // Check the version
180         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
181         {
182             msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
183                      attr["version"], SKINS_DTD_VERSION );
184             m_errors = true;
185             return;
186         }
187         const BuilderData::Theme theme( attr["tooltipfont"],
188                 atoi( attr["magnet"] ), atoi( attr["alpha"] ),
189                 atoi( attr["movealpha"] ), atoi( attr["fadetime"] ) );
190         m_data.m_listTheme.push_back( theme );
191     }
192
193     else if( rName == "ThemeInfo" )
194     {
195         msg_Warn( getIntf(), "skin: %s  author: %s", attr["name"],
196                   attr["author"] );
197     }
198
199     else if( rName == "Video" )
200     {
201         const BuilderData::Video videoData( uniqueId( attr["id"] ),
202                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
203                 atoi( attr["width"] ), atoi( attr["height" ]),
204                 attr["lefttop"], attr["rightbottom"],
205                 attr["visible"], attr["help"], m_curLayer,
206                 m_curWindowId, m_curLayoutId );
207         m_curLayer++;
208         m_data.m_listVideo.push_back( videoData );
209     }
210
211     else if( rName == "Window" )
212     {
213         m_curWindowId = uniqueId( attr["id"] );
214         const BuilderData::Window window( m_curWindowId,
215                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
216                 ConvertBoolean( attr["visible"] ),
217                 ConvertBoolean( attr["dragdrop"] ),
218                 ConvertBoolean( attr["playondrop"] ) );
219         m_data.m_listWindow.push_back( window );
220     }
221 }
222
223
224 void SkinParser::handleEndElement( const string &rName )
225 {
226     if( rName == "Group" )
227     {
228         m_xOffset -= m_xOffsetList.back();
229         m_yOffset -= m_yOffsetList.back();
230         m_xOffsetList.pop_back();
231         m_yOffsetList.pop_back();
232     }
233
234     else if( rName == "Playlist" )
235     {
236         m_curListId = "";
237     }
238 }
239
240
241 bool SkinParser::ConvertBoolean( const char *value ) const
242 {
243     return strcmp( value, "true" ) == 0;
244 }
245
246
247 int SkinParser::ConvertColor( const char *transcolor ) const
248 {
249     unsigned long iRed, iGreen, iBlue;
250     iRed = iGreen = iBlue = 0;
251     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
252     return ( iRed << 16 | iGreen << 8 | iBlue );
253 }
254
255 const string SkinParser::generateId() const
256 {
257     static int i = 1;
258
259     char genId[5];
260     snprintf( genId, 4, "%i", i++ );
261
262     string base = "_ReservedId_" + (string)genId;
263
264     return base;
265 }
266
267
268 const string SkinParser::uniqueId( const string &id )
269 {
270     string newId;
271
272     if( m_idSet.find( id ) != m_idSet.end() )
273     {
274         // The id was already used
275         if( id != "none" )
276         {
277             msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
278         }
279         newId = generateId();
280     }
281     else
282     {
283         // OK, this is a new id
284         newId = id;
285     }
286
287     // Add the id to the set
288     m_idSet.insert( newId );
289
290     return newId;
291 }
292