1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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 m_curBitmapId = uniqueId( attr["id"] );
68 const BuilderData::Bitmap bitmap( m_curBitmapId,
69 convertFileName( attr["file"] ),
70 convertColor( attr["alphacolor"] ) );
71 m_data.m_listBitmap.push_back( bitmap );
74 else if( rName == "SubBitmap" )
76 RequireDefault( "id" );
77 RequireDefault( "x" );
78 RequireDefault( "y" );
79 RequireDefault( "width" );
80 RequireDefault( "height" );
82 const BuilderData::SubBitmap bitmap( uniqueId( attr["id"] ),
83 m_curBitmapId, atoi( attr["x"] ), atoi( attr["y"] ),
84 atoi( attr["width"] ), atoi( attr["height"] ) );
85 m_data.m_listSubBitmap.push_back( bitmap );
88 else if( rName == "BitmapFont" )
90 RequireDefault( "id" );
91 RequireDefault( "file" );
92 CheckDefault( "type", "digits" );
94 const BuilderData::BitmapFont font( uniqueId( attr["id"] ),
95 convertFileName( attr["file"] ),
97 m_data.m_listBitmapFont.push_back( font );
100 else if( rName == "Button" )
102 RequireDefault( "up" );
103 CheckDefault( "id", "none" );
104 CheckDefault( "visible", "true" );
105 CheckDefault( "x", "0" );
106 CheckDefault( "y", "0" );
107 CheckDefault( "lefttop", "lefttop" );
108 CheckDefault( "rightbottom", "lefttop" );
109 CheckDefault( "down", "none" );
110 CheckDefault( "over", "none" );
111 CheckDefault( "action", "none" );
112 CheckDefault( "tooltiptext", "" );
113 CheckDefault( "help", "" );
115 const BuilderData::Button button( uniqueId( attr["id"] ),
116 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
117 attr["lefttop"], attr["rightbottom"], attr["visible"],
118 attr["up"], attr["down"], attr["over"], attr["action"],
119 attr["tooltiptext"], attr["help"],
120 m_curLayer, m_curWindowId, m_curLayoutId );
122 m_data.m_listButton.push_back( button );
125 else if( rName == "Checkbox" )
127 RequireDefault( "up1" );
128 RequireDefault( "up2" );
129 RequireDefault( "state" );
130 CheckDefault( "id", "none" );
131 CheckDefault( "visible", "true" );
132 CheckDefault( "x", "0" );
133 CheckDefault( "y", "0" );
134 CheckDefault( "lefttop", "lefttop" );
135 CheckDefault( "rightbottom", "lefttop" );
136 CheckDefault( "down1", "none" );
137 CheckDefault( "over1", "none" );
138 CheckDefault( "down2", "none" );
139 CheckDefault( "over2", "none" );
140 CheckDefault( "action1", "none" );
141 CheckDefault( "action2", "none" );
142 CheckDefault( "tooltiptext1", "" );
143 CheckDefault( "tooltiptext2", "" );
144 CheckDefault( "help", "" );
146 const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
147 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
148 attr["lefttop"], attr["rightbottom"], attr["visible"],
149 attr["up1"], attr["down1"], attr["over1"],
150 attr["up2"], attr["down2"], attr["over2"], attr["state"],
151 attr["action1"], attr["action2"], attr["tooltiptext1"],
152 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
155 m_data.m_listCheckbox.push_back( checkbox );
158 else if( rName == "Font" )
160 RequireDefault( "id" );
161 RequireDefault( "file" );
162 CheckDefault( "size", "12" );
164 const BuilderData::Font fontData( uniqueId( attr["id"] ),
165 convertFileName( attr["file"] ),
166 atoi( attr["size"] ) );
167 m_data.m_listFont.push_back( fontData );
170 else if( rName == "Group" )
172 CheckDefault( "x", "0" );
173 CheckDefault( "y", "0" );
175 m_xOffset += atoi( attr["x"] );
176 m_yOffset += atoi( attr["y"] );
177 m_xOffsetList.push_back( atoi( attr["x"] ) );
178 m_yOffsetList.push_back( atoi( attr["y"] ) );
181 else if( rName == "Image" )
183 RequireDefault( "image" );
184 CheckDefault( "id", "none" );
185 CheckDefault( "visible", "true" );
186 CheckDefault( "x", "0" );
187 CheckDefault( "y", "0" );
188 CheckDefault( "lefttop", "lefttop" );
189 CheckDefault( "rightbottom", "lefttop" );
190 CheckDefault( "action", "none" );
191 CheckDefault( "resize", "mosaic" );
192 CheckDefault( "help", "" );
194 const BuilderData::Image imageData( uniqueId( attr["id"] ),
195 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
196 attr["lefttop"], attr["rightbottom"], attr["visible"],
197 attr["image"], attr["action"], attr["resize"], attr["help"],
198 m_curLayer, m_curWindowId, m_curLayoutId );
200 m_data.m_listImage.push_back( imageData );
203 else if( rName == "Layout" )
205 RequireDefault( "width" );
206 RequireDefault( "height" );
207 CheckDefault( "id", "none" );
208 CheckDefault( "minwidth", "-1" );
209 CheckDefault( "maxwidth", "-1" );
210 CheckDefault( "minheight", "-1" );
211 CheckDefault( "maxheight", "-1" );
213 m_curLayoutId = uniqueId( attr["id"] );
214 const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
215 atoi( attr["height"] ), atoi( attr["minwidth"] ),
216 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
217 atoi( attr["maxheight"] ), m_curWindowId );
218 m_data.m_listLayout.push_back( layout );
222 else if( rName == "Playlist" )
224 RequireDefault( "id" );
225 RequireDefault( "font" );
226 CheckDefault( "visible", "true" );
227 CheckDefault( "x", "0" );
228 CheckDefault( "y", "0" );
229 CheckDefault( "width", "0" );
230 CheckDefault( "height", "0" );
231 CheckDefault( "lefttop", "lefttop" );
232 CheckDefault( "rightbottom", "lefttop" );
233 CheckDefault( "bgimage", "none" );
234 CheckDefault( "fgcolor", "#000000" );
235 CheckDefault( "playcolor", "#FF0000" );
236 CheckDefault( "bgcolor1", "#FFFFFF" );
237 CheckDefault( "bgcolor2", "#FFFFFF" );
238 CheckDefault( "selcolor", "#0000FF" );
239 CheckDefault( "help", "" );
241 m_curListId = uniqueId( attr["id"] );
242 const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
243 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
244 atoi( attr["width"]), atoi( attr["height"] ),
245 attr["lefttop"], attr["rightbottom"],
246 attr["font"], "playlist", attr["bgimage"],
247 convertColor( attr["fgcolor"] ),
248 convertColor( attr["playcolor"] ),
249 convertColor( attr["bgcolor1"] ),
250 convertColor( attr["bgcolor2"] ),
251 convertColor( attr["selcolor"] ), attr["help"],
252 m_curLayer, m_curWindowId, m_curLayoutId );
254 m_data.m_listList.push_back( listData );
257 else if( rName == "Playtree" )
259 RequireDefault( "id" );
260 RequireDefault( "font" );
261 CheckDefault( "visible", "true" );
262 CheckDefault( "x", "0" );
263 CheckDefault( "y", "0" );
264 CheckDefault( "width", "0" );
265 CheckDefault( "height", "0" );
266 CheckDefault( "lefttop", "lefttop" );
267 CheckDefault( "rightbottom", "lefttop" );
268 CheckDefault( "bgimage", "none" );
269 CheckDefault( "itemimage", "none" );
270 CheckDefault( "openimage", "none" );
271 CheckDefault( "closedimage", "none" );
272 CheckDefault( "fgcolor", "#000000" );
273 CheckDefault( "playcolor", "#FF0000" );
274 CheckDefault( "bgcolor1", "#FFFFFF" );
275 CheckDefault( "bgcolor2", "#FFFFFF" );
276 CheckDefault( "selcolor", "#0000FF" );
277 CheckDefault( "help", "" );
279 m_curTreeId = uniqueId( attr["id"] );
280 const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
281 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
282 atoi( attr["width"]), atoi( attr["height"] ),
283 attr["lefttop"], attr["rightbottom"],
284 attr["font"], "playtree",
285 attr["bgimage"], attr["itemimage"],
286 attr["openimage"], attr["closedimage"],
287 convertColor( attr["fgcolor"] ),
288 convertColor( attr["playcolor"] ),
289 convertColor( attr["bgcolor1"] ),
290 convertColor( attr["bgcolor2"] ),
291 convertColor( attr["selcolor"] ), attr["help"],
292 m_curLayer, m_curWindowId, m_curLayoutId );
294 m_data.m_listTree.push_back( treeData );
297 else if( rName == "RadialSlider" )
299 RequireDefault( "sequence" );
300 RequireDefault( "nbimages" );
301 CheckDefault( "id", "none" );
302 CheckDefault( "visible", "true" );
303 CheckDefault( "x", "0" );
304 CheckDefault( "y", "0" );
305 CheckDefault( "lefttop", "lefttop" );
306 CheckDefault( "rightbottom", "lefttop" );
307 CheckDefault( "minangle", "0" );
308 CheckDefault( "maxangle", "360" );
309 CheckDefault( "value", "none" );
310 CheckDefault( "tooltiptext", "" );
311 CheckDefault( "help", "" );
313 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
315 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
316 attr["lefttop"], attr["rightbottom"], attr["sequence"],
317 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
318 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
319 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
322 m_data.m_listRadialSlider.push_back( radial );
325 else if( rName == "Slider" )
327 RequireDefault( "up" );
328 RequireDefault( "points" );
329 CheckDefault( "id", "none" );
330 CheckDefault( "visible", "true" );
331 CheckDefault( "x", "0" );
332 CheckDefault( "y", "0" );
333 CheckDefault( "width", "0" );
334 CheckDefault( "height", "0" );
335 CheckDefault( "lefttop", "lefttop" );
336 CheckDefault( "rightbottom", "lefttop" );
337 CheckDefault( "down", "none" );
338 CheckDefault( "over", "none" );
339 CheckDefault( "thickness", "10" );
340 CheckDefault( "value", "none" );
341 CheckDefault( "tooltiptext", "" );
342 CheckDefault( "help", "" );
344 string newValue = attr["value"];
345 if( m_curListId != "" )
347 // Slider associated to a list
348 newValue = "playlist.slider";
350 else if( m_curTreeId != "" )
352 // Slider associated to a tree
353 newValue = "playtree.slider";
355 const BuilderData::Slider slider( uniqueId( attr["id"] ),
357 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
358 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
359 attr["over"], attr["points"], atoi( attr["thickness"] ),
360 newValue, attr["tooltiptext"], attr["help"], m_curLayer,
361 m_curWindowId, m_curLayoutId );
363 m_data.m_listSlider.push_back( slider );
366 else if( rName == "Text" )
368 RequireDefault( "font" );
369 CheckDefault( "id", "none" );
370 CheckDefault( "visible", "true" );
371 CheckDefault( "x", "0" );
372 CheckDefault( "y", "0" );
373 CheckDefault( "text", "" );
374 CheckDefault( "color", "#000000" );
375 CheckDefault( "width", "0" );
376 CheckDefault( "lefttop", "lefttop" );
377 CheckDefault( "rightbottom", "lefttop" );
378 CheckDefault( "help", "" );
380 const BuilderData::Text textData( uniqueId( attr["id"] ),
381 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
382 attr["visible"], attr["font"],
383 attr["text"], atoi( attr["width"] ),
384 attr["lefttop"], attr["rightbottom"],
385 convertColor( attr["color"] ), attr["help"], m_curLayer,
386 m_curWindowId, m_curLayoutId );
388 m_data.m_listText.push_back( textData );
391 else if( rName == "Theme" )
393 RequireDefault( "version" );
394 CheckDefault( "tooltipfont", "defaultfont" );
395 CheckDefault( "magnet", "15" );
396 CheckDefault( "alpha", "255" );
397 CheckDefault( "movealpha", "255" );
400 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
402 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
403 attr["version"], SKINS_DTD_VERSION );
407 const BuilderData::Theme theme( attr["tooltipfont"],
408 atoi( attr["magnet"] ),
409 convertInRange( attr["alpha"], 1, 255, "alpha" ),
410 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
411 m_data.m_listTheme.push_back( theme );
414 else if( rName == "ThemeInfo" )
416 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
420 else if( rName == "Video" )
422 CheckDefault( "id", "none" );
423 CheckDefault( "visible", "true" );
424 CheckDefault( "x", "0" );
425 CheckDefault( "y", "0" );
426 CheckDefault( "width", "0" );
427 CheckDefault( "height", "0" );
428 CheckDefault( "lefttop", "lefttop" );
429 CheckDefault( "rightbottom", "lefttop" );
430 CheckDefault( "autoresize", "false" );
431 CheckDefault( "help", "" );
433 const BuilderData::Video videoData( uniqueId( attr["id"] ),
434 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
435 atoi( attr["width"] ), atoi( attr["height" ]),
436 attr["lefttop"], attr["rightbottom"],
437 attr["visible"], convertBoolean( attr["autoresize"] ),
438 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
440 m_data.m_listVideo.push_back( videoData );
443 else if( rName == "Window" )
445 CheckDefault( "id", "none" );
446 CheckDefault( "visible", "true" );
447 CheckDefault( "x", "0" );
448 CheckDefault( "y", "0" );
449 CheckDefault( "dragdrop", "true" );
450 CheckDefault( "playondrop", "true" );
452 m_curWindowId = uniqueId( attr["id"] );
453 const BuilderData::Window window( m_curWindowId,
454 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
455 convertBoolean( attr["visible"] ),
456 convertBoolean( attr["dragdrop"] ),
457 convertBoolean( attr["playondrop"] ) );
458 m_data.m_listWindow.push_back( window );
463 void SkinParser::handleEndElement( const string &rName )
465 if( rName == "Group" )
467 m_xOffset -= m_xOffsetList.back();
468 m_yOffset -= m_yOffsetList.back();
469 m_xOffsetList.pop_back();
470 m_yOffsetList.pop_back();
472 else if( rName == "Playlist" )
476 else if( rName == "Playtree" )
483 bool SkinParser::convertBoolean( const char *value ) const
485 return strcmp( value, "true" ) == 0;
489 int SkinParser::convertColor( const char *transcolor ) const
491 unsigned long iRed, iGreen, iBlue;
492 iRed = iGreen = iBlue = 0;
493 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
494 return ( iRed << 16 | iGreen << 8 | iBlue );
498 string SkinParser::convertFileName( const char *fileName ) const
500 return m_path + string( fileName );
504 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
505 const string &rAttribute ) const
507 int intValue = atoi( value );
509 if( intValue < minValue )
511 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
512 "expected range [%i, %i], using %i instead",
513 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
516 else if( intValue > maxValue )
518 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
519 "expected range [%i, %i], using %i instead",
520 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
530 const string SkinParser::generateId() const
535 snprintf( genId, 4, "%i", i++ );
537 string base = "_ReservedId_" + (string)genId;
543 const string SkinParser::uniqueId( const string &id )
547 if( m_idSet.find( id ) != m_idSet.end() )
549 // The id was already used
552 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
554 newId = generateId();
558 // OK, this is a new id
562 // Add the id to the set
563 m_idSet.insert( newId );