1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 #include "skin_parser.hpp"
27 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName,
28 const string &rPath ):
29 XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 ),
35 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
37 #define CheckDefault( a, b ) \
38 if( attr.find(a) == attr.end() ) attr[strdup(a)] = strdup(b);
39 #define RequireDefault( a ) \
40 if( attr.find(a) == attr.end() ) \
42 msg_Err( getIntf(), "Bad theme (element: %s, missing attribute: %s)", \
44 m_errors = true; return; \
47 if( rName == "Anchor" )
49 RequireDefault( "priority" );
50 CheckDefault( "x", "0" );
51 CheckDefault( "y", "0" );
52 CheckDefault( "points", "(0,0)" );
53 CheckDefault( "range", "10" );
55 const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
56 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
57 atoi( attr["priority"] ), attr["points"], m_curLayoutId );
58 m_data.m_listAnchor.push_back( anchor );
61 else if( rName == "Bitmap" )
63 RequireDefault( "id" );
64 RequireDefault( "file" );
65 RequireDefault( "alphacolor" );
67 const BuilderData::Bitmap bitmap( uniqueId( attr["id"] ),
68 convertFileName( attr["file"] ),
69 convertColor( attr["alphacolor"] ) );
70 m_data.m_listBitmap.push_back( bitmap );
73 else if( rName == "BitmapFont" )
75 RequireDefault( "id" );
76 RequireDefault( "file" );
77 CheckDefault( "type", "digits" );
79 const BuilderData::BitmapFont font( uniqueId( attr["id"] ),
80 convertFileName( attr["file"] ),
82 m_data.m_listBitmapFont.push_back( font );
85 else if( rName == "Button" )
87 RequireDefault( "up" );
88 CheckDefault( "id", "none" );
89 CheckDefault( "visible", "true" );
90 CheckDefault( "x", "0" );
91 CheckDefault( "y", "0" );
92 CheckDefault( "lefttop", "lefttop" );
93 CheckDefault( "rightbottom", "lefttop" );
94 CheckDefault( "down", "none" );
95 CheckDefault( "over", "none" );
96 CheckDefault( "action", "none" );
97 CheckDefault( "tooltiptext", "" );
98 CheckDefault( "help", "" );
100 const BuilderData::Button button( uniqueId( attr["id"] ),
101 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
102 attr["lefttop"], attr["rightbottom"], attr["visible"],
103 attr["up"], attr["down"], attr["over"], attr["action"],
104 attr["tooltiptext"], attr["help"],
105 m_curLayer, m_curWindowId, m_curLayoutId );
107 m_data.m_listButton.push_back( button );
110 else if( rName == "Checkbox" )
112 RequireDefault( "up1" );
113 RequireDefault( "up2" );
114 RequireDefault( "state" );
115 CheckDefault( "id", "none" );
116 CheckDefault( "visible", "true" );
117 CheckDefault( "x", "0" );
118 CheckDefault( "y", "0" );
119 CheckDefault( "lefttop", "lefttop" );
120 CheckDefault( "rightbottom", "lefttop" );
121 CheckDefault( "down1", "none" );
122 CheckDefault( "over1", "none" );
123 CheckDefault( "down2", "none" );
124 CheckDefault( "over2", "none" );
125 CheckDefault( "action1", "none" );
126 CheckDefault( "action2", "none" );
127 CheckDefault( "tooltiptext1", "" );
128 CheckDefault( "tooltiptext2", "" );
129 CheckDefault( "help", "" );
131 const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
132 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
133 attr["lefttop"], attr["rightbottom"], attr["visible"],
134 attr["up1"], attr["down1"], attr["over1"],
135 attr["up2"], attr["down2"], attr["over2"], attr["state"],
136 attr["action1"], attr["action2"], attr["tooltiptext1"],
137 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
140 m_data.m_listCheckbox.push_back( checkbox );
143 else if( rName == "Font" )
145 RequireDefault( "id" );
146 RequireDefault( "file" );
147 CheckDefault( "size", "12" );
149 const BuilderData::Font fontData( uniqueId( attr["id"] ),
150 convertFileName( attr["file"] ),
151 atoi( attr["size"] ) );
152 m_data.m_listFont.push_back( fontData );
155 else if( rName == "Group" )
157 CheckDefault( "x", "0" );
158 CheckDefault( "y", "0" );
160 m_xOffset += atoi( attr["x"] );
161 m_yOffset += atoi( attr["y"] );
162 m_xOffsetList.push_back( atoi( attr["x"] ) );
163 m_yOffsetList.push_back( atoi( attr["y"] ) );
166 else if( rName == "Image" )
168 RequireDefault( "image" );
169 CheckDefault( "id", "none" );
170 CheckDefault( "visible", "true" );
171 CheckDefault( "x", "0" );
172 CheckDefault( "y", "0" );
173 CheckDefault( "lefttop", "lefttop" );
174 CheckDefault( "rightbottom", "lefttop" );
175 CheckDefault( "action", "none" );
176 CheckDefault( "resize", "mosaic" );
177 CheckDefault( "help", "" );
179 const BuilderData::Image imageData( uniqueId( attr["id"] ),
180 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
181 attr["lefttop"], attr["rightbottom"], attr["visible"],
182 attr["image"], attr["action"], attr["resize"], attr["help"],
183 m_curLayer, m_curWindowId, m_curLayoutId );
185 m_data.m_listImage.push_back( imageData );
188 else if( rName == "Layout" )
190 RequireDefault( "width" );
191 RequireDefault( "height" );
192 CheckDefault( "id", "none" );
193 CheckDefault( "minwidth", "-1" );
194 CheckDefault( "maxwidth", "-1" );
195 CheckDefault( "minheight", "-1" );
196 CheckDefault( "maxheight", "-1" );
198 m_curLayoutId = uniqueId( attr["id"] );
199 const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
200 atoi( attr["height"] ), atoi( attr["minwidth"] ),
201 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
202 atoi( attr["maxheight"] ), m_curWindowId );
203 m_data.m_listLayout.push_back( layout );
207 else if( rName == "Playlist" )
209 RequireDefault( "id" );
210 RequireDefault( "font" );
211 CheckDefault( "visible", "true" );
212 CheckDefault( "x", "0" );
213 CheckDefault( "y", "0" );
214 CheckDefault( "width", "0" );
215 CheckDefault( "height", "0" );
216 CheckDefault( "lefttop", "lefttop" );
217 CheckDefault( "rightbottom", "lefttop" );
218 CheckDefault( "fgcolor", "#000000" );
219 CheckDefault( "playcolor", "#FF0000" );
220 CheckDefault( "bgcolor1", "#FFFFFF" );
221 CheckDefault( "bgcolor2", "#FFFFFF" );
222 CheckDefault( "selcolor", "#0000FF" );
223 CheckDefault( "help", "" );
225 m_curListId = uniqueId( attr["id"] );
226 const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
227 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
228 atoi( attr["width"]), atoi( attr["height"] ),
229 attr["lefttop"], attr["rightbottom"],
230 attr["font"], "playlist", convertColor( attr["fgcolor"] ),
231 convertColor( attr["playcolor"] ),
232 convertColor( attr["bgcolor1"] ),
233 convertColor( attr["bgcolor2"] ),
234 convertColor( attr["selcolor"] ), attr["help"],
235 m_curLayer, m_curWindowId, m_curLayoutId );
237 m_data.m_listList.push_back( listData );
240 else if( rName == "RadialSlider" )
242 RequireDefault( "sequence" );
243 RequireDefault( "nbimages" );
244 CheckDefault( "id", "none" );
245 CheckDefault( "visible", "true" );
246 CheckDefault( "x", "0" );
247 CheckDefault( "y", "0" );
248 CheckDefault( "lefttop", "lefttop" );
249 CheckDefault( "rightbottom", "lefttop" );
250 CheckDefault( "minangle", "0" );
251 CheckDefault( "maxangle", "360" );
252 CheckDefault( "value", "none" );
253 CheckDefault( "tooltiptext", "" );
254 CheckDefault( "help", "" );
256 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
258 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
259 attr["lefttop"], attr["rightbottom"], attr["sequence"],
260 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
261 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
262 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
265 m_data.m_listRadialSlider.push_back( radial );
268 else if( rName == "Slider" )
270 RequireDefault( "up" );
271 RequireDefault( "points" );
272 CheckDefault( "id", "none" );
273 CheckDefault( "visible", "true" );
274 CheckDefault( "x", "0" );
275 CheckDefault( "y", "0" );
276 CheckDefault( "width", "0" );
277 CheckDefault( "height", "0" );
278 CheckDefault( "lefttop", "lefttop" );
279 CheckDefault( "rightbottom", "lefttop" );
280 CheckDefault( "down", "none" );
281 CheckDefault( "over", "none" );
282 CheckDefault( "thickness", "10" );
283 CheckDefault( "value", "none" );
284 CheckDefault( "tooltiptext", "" );
285 CheckDefault( "help", "" );
287 string newValue = attr["value"];
288 if( m_curListId != "" )
290 // Slider associated to a list
291 newValue = "playlist.slider";
293 const BuilderData::Slider slider( uniqueId( attr["id"] ),
295 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
296 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
297 attr["over"], attr["points"], atoi( attr["thickness"] ),
298 newValue, attr["tooltiptext"], attr["help"], m_curLayer,
299 m_curWindowId, m_curLayoutId );
301 m_data.m_listSlider.push_back( slider );
304 else if( rName == "Text" )
306 RequireDefault( "font" );
307 CheckDefault( "id", "none" );
308 CheckDefault( "visible", "true" );
309 CheckDefault( "x", "0" );
310 CheckDefault( "y", "0" );
311 CheckDefault( "text", "" );
312 CheckDefault( "color", "#000000" );
313 CheckDefault( "width", "0" );
314 CheckDefault( "lefttop", "lefttop" );
315 CheckDefault( "rightbottom", "lefttop" );
316 CheckDefault( "help", "" );
318 const BuilderData::Text textData( uniqueId( attr["id"] ),
319 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
320 attr["visible"], attr["font"],
321 attr["text"], atoi( attr["width"] ),
322 attr["lefttop"], attr["rightbottom"],
323 convertColor( attr["color"] ), attr["help"], m_curLayer,
324 m_curWindowId, m_curLayoutId );
326 m_data.m_listText.push_back( textData );
329 else if( rName == "Theme" )
331 RequireDefault( "version" );
332 CheckDefault( "tooltipfont", "defaultfont" );
333 CheckDefault( "magnet", "15" );
334 CheckDefault( "alpha", "255" );
335 CheckDefault( "movealpha", "255" );
338 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
340 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
341 attr["version"], SKINS_DTD_VERSION );
345 const BuilderData::Theme theme( attr["tooltipfont"],
346 atoi( attr["magnet"] ),
347 convertInRange( attr["alpha"], 1, 255, "alpha" ),
348 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
349 m_data.m_listTheme.push_back( theme );
352 else if( rName == "ThemeInfo" )
354 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
358 else if( rName == "Video" )
360 CheckDefault( "id", "none" );
361 CheckDefault( "visible", "true" );
362 CheckDefault( "x", "0" );
363 CheckDefault( "y", "0" );
364 CheckDefault( "width", "0" );
365 CheckDefault( "height", "0" );
366 CheckDefault( "lefttop", "lefttop" );
367 CheckDefault( "rightbottom", "lefttop" );
368 CheckDefault( "help", "" );
370 const BuilderData::Video videoData( uniqueId( attr["id"] ),
371 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
372 atoi( attr["width"] ), atoi( attr["height" ]),
373 attr["lefttop"], attr["rightbottom"],
374 attr["visible"], attr["help"], m_curLayer,
375 m_curWindowId, m_curLayoutId );
377 m_data.m_listVideo.push_back( videoData );
380 else if( rName == "Window" )
382 CheckDefault( "id", "none" );
383 CheckDefault( "visible", "true" );
384 CheckDefault( "x", "0" );
385 CheckDefault( "y", "0" );
386 CheckDefault( "dragdrop", "true" );
387 CheckDefault( "playondrop", "true" );
389 m_curWindowId = uniqueId( attr["id"] );
390 const BuilderData::Window window( m_curWindowId,
391 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
392 convertBoolean( attr["visible"] ),
393 convertBoolean( attr["dragdrop"] ),
394 convertBoolean( attr["playondrop"] ) );
395 m_data.m_listWindow.push_back( window );
400 void SkinParser::handleEndElement( const string &rName )
402 if( rName == "Group" )
404 m_xOffset -= m_xOffsetList.back();
405 m_yOffset -= m_yOffsetList.back();
406 m_xOffsetList.pop_back();
407 m_yOffsetList.pop_back();
410 else if( rName == "Playlist" )
417 bool SkinParser::convertBoolean( const char *value ) const
419 return strcmp( value, "true" ) == 0;
423 int SkinParser::convertColor( const char *transcolor ) const
425 unsigned long iRed, iGreen, iBlue;
426 iRed = iGreen = iBlue = 0;
427 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
428 return ( iRed << 16 | iGreen << 8 | iBlue );
432 string SkinParser::convertFileName( const char *fileName ) const
434 return m_path + string( fileName );
438 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
439 const string &rAttribute ) const
441 int intValue = atoi( value );
443 if( intValue < minValue )
445 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
446 "expected range [%i, %i], using %i instead",
447 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
450 else if( intValue > maxValue )
452 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
453 "expected range [%i, %i], using %i instead",
454 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
464 const string SkinParser::generateId() const
469 snprintf( genId, 4, "%i", i++ );
471 string base = "_ReservedId_" + (string)genId;
477 const string SkinParser::uniqueId( const string &id )
481 if( m_idSet.find( id ) != m_idSet.end() )
483 // The id was already used
486 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
488 newId = generateId();
492 // OK, this is a new id
496 // Add the id to the set
497 m_idSet.insert( newId );