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"
25 #include "../src/os_factory.hpp"
28 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName,
29 const string &rPath, bool useDTD, BuilderData *pData ):
30 XMLParser( pIntf, rFileName, useDTD ), m_pData(pData),
31 m_ownData(pData == NULL), m_xOffset( 0 ), m_yOffset( 0 ), m_path( rPath )
33 // Make sure the data is allocated
36 m_pData = new BuilderData();
41 SkinParser::~SkinParser()
50 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
52 #define CheckDefault( a, b ) \
53 if( attr.find(a) == attr.end() ) attr[strdup(a)] = strdup(b);
54 #define RequireDefault( a ) \
55 if( attr.find(a) == attr.end() ) \
57 msg_Err( getIntf(), "Bad theme (element: %s, missing attribute: %s)", \
59 m_errors = true; return; \
62 if( rName == "Include" )
64 RequireDefault( "file" );
65 msg_Dbg( getIntf(), "Opening included XML file: %s",
66 convertFileName( attr["file"] ).c_str() );
67 // FIXME: We do not use the DTD to validate the included XML file,
68 // as the parser seems to dislike it otherwise...
69 SkinParser subParser( getIntf(), convertFileName( attr["file"] ),
70 m_path, false, m_pData );
74 else if( rName == "Anchor" )
76 RequireDefault( "priority" );
77 CheckDefault( "x", "0" );
78 CheckDefault( "y", "0" );
79 CheckDefault( "points", "(0,0)" );
80 CheckDefault( "range", "10" );
82 const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
83 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
84 atoi( attr["priority"] ), attr["points"], m_curLayoutId );
85 m_pData->m_listAnchor.push_back( anchor );
88 else if( rName == "Bitmap" )
90 RequireDefault( "id" );
91 RequireDefault( "file" );
92 RequireDefault( "alphacolor" );
94 m_curBitmapId = uniqueId( attr["id"] );
95 const BuilderData::Bitmap bitmap( m_curBitmapId,
96 convertFileName( attr["file"] ),
97 convertColor( attr["alphacolor"] ) );
98 m_pData->m_listBitmap.push_back( bitmap );
101 else if( rName == "SubBitmap" )
103 RequireDefault( "id" );
104 RequireDefault( "x" );
105 RequireDefault( "y" );
106 RequireDefault( "width" );
107 RequireDefault( "height" );
109 const BuilderData::SubBitmap bitmap( uniqueId( attr["id"] ),
110 m_curBitmapId, atoi( attr["x"] ), atoi( attr["y"] ),
111 atoi( attr["width"] ), atoi( attr["height"] ) );
112 m_pData->m_listSubBitmap.push_back( bitmap );
115 else if( rName == "BitmapFont" )
117 RequireDefault( "id" );
118 RequireDefault( "file" );
119 CheckDefault( "type", "digits" );
121 const BuilderData::BitmapFont font( attr["id"],
122 convertFileName( attr["file"] ),
124 m_pData->m_listBitmapFont.push_back( font );
127 else if( rName == "Button" )
129 RequireDefault( "up" );
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( "down", "none" );
137 CheckDefault( "over", "none" );
138 CheckDefault( "action", "none" );
139 CheckDefault( "tooltiptext", "" );
140 CheckDefault( "help", "" );
142 const BuilderData::Button button( uniqueId( attr["id"] ),
143 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
144 attr["lefttop"], attr["rightbottom"], attr["visible"],
145 attr["up"], attr["down"], attr["over"], attr["action"],
146 attr["tooltiptext"], attr["help"],
147 m_curLayer, m_curWindowId, m_curLayoutId );
149 m_pData->m_listButton.push_back( button );
152 else if( rName == "Checkbox" )
154 RequireDefault( "up1" );
155 RequireDefault( "up2" );
156 RequireDefault( "state" );
157 CheckDefault( "id", "none" );
158 CheckDefault( "visible", "true" );
159 CheckDefault( "x", "0" );
160 CheckDefault( "y", "0" );
161 CheckDefault( "lefttop", "lefttop" );
162 CheckDefault( "rightbottom", "lefttop" );
163 CheckDefault( "down1", "none" );
164 CheckDefault( "over1", "none" );
165 CheckDefault( "down2", "none" );
166 CheckDefault( "over2", "none" );
167 CheckDefault( "action1", "none" );
168 CheckDefault( "action2", "none" );
169 CheckDefault( "tooltiptext1", "" );
170 CheckDefault( "tooltiptext2", "" );
171 CheckDefault( "help", "" );
173 const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
174 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
175 attr["lefttop"], attr["rightbottom"], attr["visible"],
176 attr["up1"], attr["down1"], attr["over1"],
177 attr["up2"], attr["down2"], attr["over2"], attr["state"],
178 attr["action1"], attr["action2"], attr["tooltiptext1"],
179 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
182 m_pData->m_listCheckbox.push_back( checkbox );
185 else if( rName == "Font" )
187 RequireDefault( "id" );
188 RequireDefault( "file" );
189 CheckDefault( "size", "12" );
191 const BuilderData::Font fontData( uniqueId( attr["id"] ),
192 convertFileName( attr["file"] ),
193 atoi( attr["size"] ) );
194 m_pData->m_listFont.push_back( fontData );
197 else if( rName == "Group" )
199 CheckDefault( "x", "0" );
200 CheckDefault( "y", "0" );
202 m_xOffset += atoi( attr["x"] );
203 m_yOffset += atoi( attr["y"] );
204 m_xOffsetList.push_back( atoi( attr["x"] ) );
205 m_yOffsetList.push_back( atoi( attr["y"] ) );
208 else if( rName == "Image" )
210 RequireDefault( "image" );
211 CheckDefault( "id", "none" );
212 CheckDefault( "visible", "true" );
213 CheckDefault( "x", "0" );
214 CheckDefault( "y", "0" );
215 CheckDefault( "lefttop", "lefttop" );
216 CheckDefault( "rightbottom", "lefttop" );
217 CheckDefault( "action", "none" );
218 CheckDefault( "resize", "mosaic" );
219 CheckDefault( "help", "" );
221 const BuilderData::Image imageData( uniqueId( attr["id"] ),
222 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
223 attr["lefttop"], attr["rightbottom"], attr["visible"],
224 attr["image"], attr["action"], attr["resize"], attr["help"],
225 m_curLayer, m_curWindowId, m_curLayoutId );
227 m_pData->m_listImage.push_back( imageData );
230 else if( rName == "Layout" )
232 RequireDefault( "width" );
233 RequireDefault( "height" );
234 CheckDefault( "id", "none" );
235 CheckDefault( "minwidth", "-1" );
236 CheckDefault( "maxwidth", "-1" );
237 CheckDefault( "minheight", "-1" );
238 CheckDefault( "maxheight", "-1" );
240 m_curLayoutId = uniqueId( attr["id"] );
241 const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
242 atoi( attr["height"] ), atoi( attr["minwidth"] ),
243 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
244 atoi( attr["maxheight"] ), m_curWindowId );
245 m_pData->m_listLayout.push_back( layout );
249 else if( rName == "Playlist" )
251 RequireDefault( "id" );
252 RequireDefault( "font" );
253 CheckDefault( "visible", "true" );
254 CheckDefault( "x", "0" );
255 CheckDefault( "y", "0" );
256 CheckDefault( "width", "0" );
257 CheckDefault( "height", "0" );
258 CheckDefault( "lefttop", "lefttop" );
259 CheckDefault( "rightbottom", "lefttop" );
260 CheckDefault( "bgimage", "none" );
261 CheckDefault( "fgcolor", "#000000" );
262 CheckDefault( "playcolor", "#FF0000" );
263 CheckDefault( "bgcolor1", "#FFFFFF" );
264 CheckDefault( "bgcolor2", "#FFFFFF" );
265 CheckDefault( "selcolor", "#0000FF" );
266 CheckDefault( "help", "" );
268 m_curListId = uniqueId( attr["id"] );
269 const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
270 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
271 atoi( attr["width"]), atoi( attr["height"] ),
272 attr["lefttop"], attr["rightbottom"],
273 attr["font"], "playlist", attr["bgimage"],
274 convertColor( attr["fgcolor"] ),
275 convertColor( attr["playcolor"] ),
276 convertColor( attr["bgcolor1"] ),
277 convertColor( attr["bgcolor2"] ),
278 convertColor( attr["selcolor"] ), attr["help"],
279 m_curLayer, m_curWindowId, m_curLayoutId );
281 m_pData->m_listList.push_back( listData );
284 else if( rName == "Playtree" )
286 RequireDefault( "id" );
287 RequireDefault( "font" );
288 CheckDefault( "visible", "true" );
289 CheckDefault( "x", "0" );
290 CheckDefault( "y", "0" );
291 CheckDefault( "width", "0" );
292 CheckDefault( "height", "0" );
293 CheckDefault( "lefttop", "lefttop" );
294 CheckDefault( "rightbottom", "lefttop" );
295 CheckDefault( "bgimage", "none" );
296 CheckDefault( "itemimage", "none" );
297 CheckDefault( "openimage", "none" );
298 CheckDefault( "closedimage", "none" );
299 CheckDefault( "fgcolor", "#000000" );
300 CheckDefault( "playcolor", "#FF0000" );
301 CheckDefault( "bgcolor1", "#FFFFFF" );
302 CheckDefault( "bgcolor2", "#FFFFFF" );
303 CheckDefault( "selcolor", "#0000FF" );
304 CheckDefault( "help", "" );
306 m_curTreeId = uniqueId( attr["id"] );
307 const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
308 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
309 atoi( attr["width"]), atoi( attr["height"] ),
310 attr["lefttop"], attr["rightbottom"],
311 attr["font"], "playtree",
312 attr["bgimage"], attr["itemimage"],
313 attr["openimage"], attr["closedimage"],
314 convertColor( attr["fgcolor"] ),
315 convertColor( attr["playcolor"] ),
316 convertColor( attr["bgcolor1"] ),
317 convertColor( attr["bgcolor2"] ),
318 convertColor( attr["selcolor"] ), attr["help"],
319 m_curLayer, m_curWindowId, m_curLayoutId );
321 m_pData->m_listTree.push_back( treeData );
324 else if( rName == "RadialSlider" )
326 RequireDefault( "sequence" );
327 RequireDefault( "nbimages" );
328 CheckDefault( "id", "none" );
329 CheckDefault( "visible", "true" );
330 CheckDefault( "x", "0" );
331 CheckDefault( "y", "0" );
332 CheckDefault( "lefttop", "lefttop" );
333 CheckDefault( "rightbottom", "lefttop" );
334 CheckDefault( "minangle", "0" );
335 CheckDefault( "maxangle", "360" );
336 CheckDefault( "value", "none" );
337 CheckDefault( "tooltiptext", "" );
338 CheckDefault( "help", "" );
340 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
342 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
343 attr["lefttop"], attr["rightbottom"], attr["sequence"],
344 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
345 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
346 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
349 m_pData->m_listRadialSlider.push_back( radial );
352 else if( rName == "Slider" )
354 RequireDefault( "up" );
355 RequireDefault( "points" );
356 CheckDefault( "id", "none" );
357 CheckDefault( "visible", "true" );
358 CheckDefault( "x", "0" );
359 CheckDefault( "y", "0" );
360 CheckDefault( "width", "0" );
361 CheckDefault( "height", "0" );
362 CheckDefault( "lefttop", "lefttop" );
363 CheckDefault( "rightbottom", "lefttop" );
364 CheckDefault( "down", "none" );
365 CheckDefault( "over", "none" );
366 CheckDefault( "thickness", "10" );
367 CheckDefault( "value", "none" );
368 CheckDefault( "background", "none" );
369 CheckDefault( "nbimages", "1" );
370 CheckDefault( "tooltiptext", "" );
371 CheckDefault( "help", "" );
373 string newValue = attr["value"];
374 if( m_curListId != "" )
376 // Slider associated to a list
377 newValue = "playlist.slider";
379 else if( m_curTreeId != "" )
381 // Slider associated to a tree
382 newValue = "playtree.slider";
384 const BuilderData::Slider slider( uniqueId( attr["id"] ),
386 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
387 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
388 attr["over"], attr["points"], atoi( attr["thickness"] ),
389 newValue, attr["background"], atoi( attr["nbimages"] ),
390 attr["tooltiptext"], attr["help"], m_curLayer,
391 m_curWindowId, m_curLayoutId );
393 m_pData->m_listSlider.push_back( slider );
396 else if( rName == "Text" )
398 RequireDefault( "font" );
399 CheckDefault( "id", "none" );
400 CheckDefault( "visible", "true" );
401 CheckDefault( "x", "0" );
402 CheckDefault( "y", "0" );
403 CheckDefault( "text", "" );
404 CheckDefault( "color", "#000000" );
405 CheckDefault( "width", "0" );
406 CheckDefault( "lefttop", "lefttop" );
407 CheckDefault( "rightbottom", "lefttop" );
408 CheckDefault( "help", "" );
410 const BuilderData::Text textData( uniqueId( attr["id"] ),
411 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
412 attr["visible"], attr["font"],
413 attr["text"], atoi( attr["width"] ),
414 attr["lefttop"], attr["rightbottom"],
415 convertColor( attr["color"] ), attr["help"], m_curLayer,
416 m_curWindowId, m_curLayoutId );
418 m_pData->m_listText.push_back( textData );
421 else if( rName == "Theme" )
423 RequireDefault( "version" );
424 CheckDefault( "tooltipfont", "defaultfont" );
425 CheckDefault( "magnet", "15" );
426 CheckDefault( "alpha", "255" );
427 CheckDefault( "movealpha", "255" );
430 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
432 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
433 attr["version"], SKINS_DTD_VERSION );
437 const BuilderData::Theme theme( attr["tooltipfont"],
438 atoi( attr["magnet"] ),
439 convertInRange( attr["alpha"], 1, 255, "alpha" ),
440 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
441 m_pData->m_listTheme.push_back( theme );
444 else if( rName == "ThemeInfo" )
446 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
450 else if( rName == "Video" )
452 CheckDefault( "id", "none" );
453 CheckDefault( "visible", "true" );
454 CheckDefault( "x", "0" );
455 CheckDefault( "y", "0" );
456 CheckDefault( "width", "0" );
457 CheckDefault( "height", "0" );
458 CheckDefault( "lefttop", "lefttop" );
459 CheckDefault( "rightbottom", "lefttop" );
460 CheckDefault( "autoresize", "false" );
461 CheckDefault( "help", "" );
463 const BuilderData::Video videoData( uniqueId( attr["id"] ),
464 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
465 atoi( attr["width"] ), atoi( attr["height" ]),
466 attr["lefttop"], attr["rightbottom"],
467 attr["visible"], convertBoolean( attr["autoresize"] ),
468 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
470 m_pData->m_listVideo.push_back( videoData );
473 else if( rName == "Window" )
475 CheckDefault( "id", "none" );
476 CheckDefault( "visible", "true" );
477 CheckDefault( "x", "0" );
478 CheckDefault( "y", "0" );
479 CheckDefault( "dragdrop", "true" );
480 CheckDefault( "playondrop", "true" );
482 m_curWindowId = uniqueId( attr["id"] );
483 const BuilderData::Window window( m_curWindowId,
484 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
485 convertBoolean( attr["visible"] ),
486 convertBoolean( attr["dragdrop"] ),
487 convertBoolean( attr["playondrop"] ) );
488 m_pData->m_listWindow.push_back( window );
493 void SkinParser::handleEndElement( const string &rName )
495 if( rName == "Group" )
497 m_xOffset -= m_xOffsetList.back();
498 m_yOffset -= m_yOffsetList.back();
499 m_xOffsetList.pop_back();
500 m_yOffsetList.pop_back();
502 else if( rName == "Playlist" )
506 else if( rName == "Playtree" )
513 bool SkinParser::convertBoolean( const char *value ) const
515 return strcmp( value, "true" ) == 0;
519 int SkinParser::convertColor( const char *transcolor ) const
521 unsigned long iRed, iGreen, iBlue;
522 iRed = iGreen = iBlue = 0;
523 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
524 return ( iRed << 16 | iGreen << 8 | iBlue );
528 string SkinParser::convertFileName( const char *fileName ) const
530 OSFactory *pFactory = OSFactory::instance( getIntf() );
531 return m_path + pFactory->getDirSeparator() + string( fileName );
535 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
536 const string &rAttribute ) const
538 int intValue = atoi( value );
540 if( intValue < minValue )
542 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
543 "expected range [%i, %i], using %i instead",
544 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
547 else if( intValue > maxValue )
549 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
550 "expected range [%i, %i], using %i instead",
551 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
561 const string SkinParser::generateId() const
566 snprintf( genId, 4, "%i", i++ );
568 string base = "_ReservedId_" + (string)genId;
574 const string SkinParser::uniqueId( const string &id )
578 if( m_idSet.find( id ) != m_idSet.end() )
580 // The id was already used
583 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
585 newId = generateId();
589 // OK, this is a new id
593 // Add the id to the set
594 m_idSet.insert( newId );