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_path( rPath), m_pData(pData),
31 m_ownData(pData == NULL), m_xOffset( 0 ), m_yOffset( 0 )
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" );
66 OSFactory *pFactory = OSFactory::instance( getIntf() );
67 string fullPath = m_path + pFactory->getDirSeparator() + attr["file"];
68 msg_Dbg( getIntf(), "Opening included XML file: %s", fullPath.c_str() );
69 // FIXME: We do not use the DTD to validate the included XML file,
70 // as the parser seems to dislike it otherwise...
71 SkinParser subParser( getIntf(), fullPath.c_str(), false, m_pData );
75 else if( rName == "Anchor" )
77 RequireDefault( "priority" );
78 CheckDefault( "x", "0" );
79 CheckDefault( "y", "0" );
80 CheckDefault( "points", "(0,0)" );
81 CheckDefault( "range", "10" );
83 const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
84 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
85 atoi( attr["priority"] ), attr["points"], m_curLayoutId );
86 m_pData->m_listAnchor.push_back( anchor );
89 else if( rName == "Bitmap" )
91 RequireDefault( "id" );
92 RequireDefault( "file" );
93 RequireDefault( "alphacolor" );
94 CheckDefault( "nbFrames", "1" );
95 CheckDefault( "fps", "4" );
97 m_curBitmapId = uniqueId( attr["id"] );
98 const BuilderData::Bitmap bitmap( m_curBitmapId,
99 attr["file"], convertColor( attr["alphacolor"] ),
100 atoi( attr["nbFrames"] ), atoi( attr["fps"] ) );
101 m_pData->m_listBitmap.push_back( bitmap );
104 else if( rName == "SubBitmap" )
106 RequireDefault( "id" );
107 RequireDefault( "x" );
108 RequireDefault( "y" );
109 RequireDefault( "width" );
110 RequireDefault( "height" );
111 CheckDefault( "nbFrames", "1" );
112 CheckDefault( "fps", "4" );
114 const BuilderData::SubBitmap bitmap( attr["id"],
115 m_curBitmapId, atoi( attr["x"] ), atoi( attr["y"] ),
116 atoi( attr["width"] ), atoi( attr["height"] ),
117 atoi( attr["nbFrames"] ), atoi( attr["fps"] ) );
118 m_pData->m_listSubBitmap.push_back( bitmap );
121 else if( rName == "BitmapFont" )
123 RequireDefault( "id" );
124 RequireDefault( "file" );
125 CheckDefault( "type", "digits" );
127 const BuilderData::BitmapFont font( attr["id"],
128 attr["file"], attr["type"] );
129 m_pData->m_listBitmapFont.push_back( font );
132 else if( rName == "Button" )
134 RequireDefault( "up" );
135 CheckDefault( "id", "none" );
136 CheckDefault( "visible", "true" );
137 CheckDefault( "x", "0" );
138 CheckDefault( "y", "0" );
139 CheckDefault( "lefttop", "lefttop" );
140 CheckDefault( "rightbottom", "lefttop" );
141 CheckDefault( "down", "none" );
142 CheckDefault( "over", "none" );
143 CheckDefault( "action", "none" );
144 CheckDefault( "tooltiptext", "" );
145 CheckDefault( "help", "" );
147 const BuilderData::Button button( uniqueId( attr["id"] ),
148 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
149 attr["lefttop"], attr["rightbottom"], attr["visible"],
150 attr["up"], attr["down"], attr["over"], attr["action"],
151 attr["tooltiptext"], attr["help"],
152 m_curLayer, m_curWindowId, m_curLayoutId );
154 m_pData->m_listButton.push_back( button );
157 else if( rName == "Checkbox" )
159 RequireDefault( "up1" );
160 RequireDefault( "up2" );
161 RequireDefault( "state" );
162 CheckDefault( "id", "none" );
163 CheckDefault( "visible", "true" );
164 CheckDefault( "x", "0" );
165 CheckDefault( "y", "0" );
166 CheckDefault( "lefttop", "lefttop" );
167 CheckDefault( "rightbottom", "lefttop" );
168 CheckDefault( "down1", "none" );
169 CheckDefault( "over1", "none" );
170 CheckDefault( "down2", "none" );
171 CheckDefault( "over2", "none" );
172 CheckDefault( "action1", "none" );
173 CheckDefault( "action2", "none" );
174 CheckDefault( "tooltiptext1", "" );
175 CheckDefault( "tooltiptext2", "" );
176 CheckDefault( "help", "" );
178 const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
179 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
180 attr["lefttop"], attr["rightbottom"], attr["visible"],
181 attr["up1"], attr["down1"], attr["over1"],
182 attr["up2"], attr["down2"], attr["over2"], attr["state"],
183 attr["action1"], attr["action2"], attr["tooltiptext1"],
184 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
187 m_pData->m_listCheckbox.push_back( checkbox );
190 else if( rName == "Font" )
192 RequireDefault( "id" );
193 RequireDefault( "file" );
194 CheckDefault( "size", "12" );
196 const BuilderData::Font fontData( uniqueId( attr["id"] ),
197 attr["file"], atoi( attr["size"] ) );
198 m_pData->m_listFont.push_back( fontData );
201 else if( rName == "Group" )
203 CheckDefault( "x", "0" );
204 CheckDefault( "y", "0" );
206 m_xOffset += atoi( attr["x"] );
207 m_yOffset += atoi( attr["y"] );
208 m_xOffsetList.push_back( atoi( attr["x"] ) );
209 m_yOffsetList.push_back( atoi( attr["y"] ) );
212 else if( rName == "Image" )
214 RequireDefault( "image" );
215 CheckDefault( "id", "none" );
216 CheckDefault( "visible", "true" );
217 CheckDefault( "x", "0" );
218 CheckDefault( "y", "0" );
219 CheckDefault( "lefttop", "lefttop" );
220 CheckDefault( "rightbottom", "lefttop" );
221 CheckDefault( "action", "none" );
222 CheckDefault( "action2", "none" );
223 CheckDefault( "resize", "mosaic" );
224 CheckDefault( "help", "" );
226 const BuilderData::Image imageData( uniqueId( attr["id"] ),
227 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
228 attr["lefttop"], attr["rightbottom"], attr["visible"],
229 attr["image"], attr["action"], attr["action2"], attr["resize"],
230 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
232 m_pData->m_listImage.push_back( imageData );
235 else if( rName == "Layout" )
237 RequireDefault( "width" );
238 RequireDefault( "height" );
239 CheckDefault( "id", "none" );
240 CheckDefault( "minwidth", "-1" );
241 CheckDefault( "maxwidth", "-1" );
242 CheckDefault( "minheight", "-1" );
243 CheckDefault( "maxheight", "-1" );
245 m_curLayoutId = uniqueId( attr["id"] );
246 const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
247 atoi( attr["height"] ), atoi( attr["minwidth"] ),
248 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
249 atoi( attr["maxheight"] ), m_curWindowId );
250 m_pData->m_listLayout.push_back( layout );
254 else if( rName == "Playlist" )
256 RequireDefault( "id" );
257 RequireDefault( "font" );
258 CheckDefault( "visible", "true" );
259 CheckDefault( "x", "0" );
260 CheckDefault( "y", "0" );
261 CheckDefault( "width", "0" );
262 CheckDefault( "height", "0" );
263 CheckDefault( "lefttop", "lefttop" );
264 CheckDefault( "rightbottom", "lefttop" );
265 CheckDefault( "bgimage", "none" );
266 CheckDefault( "fgcolor", "#000000" );
267 CheckDefault( "playcolor", "#FF0000" );
268 CheckDefault( "bgcolor1", "#FFFFFF" );
269 CheckDefault( "bgcolor2", "#FFFFFF" );
270 CheckDefault( "selcolor", "#0000FF" );
271 CheckDefault( "help", "" );
273 m_curListId = uniqueId( attr["id"] );
274 const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
275 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
276 atoi( attr["width"]), atoi( attr["height"] ),
277 attr["lefttop"], attr["rightbottom"],
278 attr["font"], "playlist", attr["bgimage"],
279 convertColor( attr["fgcolor"] ),
280 convertColor( attr["playcolor"] ),
281 convertColor( attr["bgcolor1"] ),
282 convertColor( attr["bgcolor2"] ),
283 convertColor( attr["selcolor"] ), attr["help"],
284 m_curLayer, m_curWindowId, m_curLayoutId );
286 m_pData->m_listList.push_back( listData );
289 else if( rName == "Playtree" )
291 RequireDefault( "id" );
292 RequireDefault( "font" );
293 CheckDefault( "visible", "true" );
294 CheckDefault( "x", "0" );
295 CheckDefault( "y", "0" );
296 CheckDefault( "width", "0" );
297 CheckDefault( "height", "0" );
298 CheckDefault( "lefttop", "lefttop" );
299 CheckDefault( "rightbottom", "lefttop" );
300 CheckDefault( "bgimage", "none" );
301 CheckDefault( "itemimage", "none" );
302 CheckDefault( "openimage", "none" );
303 CheckDefault( "closedimage", "none" );
304 CheckDefault( "fgcolor", "#000000" );
305 CheckDefault( "playcolor", "#FF0000" );
306 CheckDefault( "bgcolor1", "#FFFFFF" );
307 CheckDefault( "bgcolor2", "#FFFFFF" );
308 CheckDefault( "selcolor", "#0000FF" );
309 CheckDefault( "help", "" );
311 m_curTreeId = uniqueId( attr["id"] );
312 const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
313 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
314 atoi( attr["width"]), atoi( attr["height"] ),
315 attr["lefttop"], attr["rightbottom"],
316 attr["font"], "playtree",
317 attr["bgimage"], attr["itemimage"],
318 attr["openimage"], attr["closedimage"],
319 convertColor( attr["fgcolor"] ),
320 convertColor( attr["playcolor"] ),
321 convertColor( attr["bgcolor1"] ),
322 convertColor( attr["bgcolor2"] ),
323 convertColor( attr["selcolor"] ), attr["help"],
324 m_curLayer, m_curWindowId, m_curLayoutId );
326 m_pData->m_listTree.push_back( treeData );
329 else if( rName == "RadialSlider" )
331 RequireDefault( "sequence" );
332 RequireDefault( "nbimages" );
333 CheckDefault( "id", "none" );
334 CheckDefault( "visible", "true" );
335 CheckDefault( "x", "0" );
336 CheckDefault( "y", "0" );
337 CheckDefault( "lefttop", "lefttop" );
338 CheckDefault( "rightbottom", "lefttop" );
339 CheckDefault( "minangle", "0" );
340 CheckDefault( "maxangle", "360" );
341 CheckDefault( "value", "none" );
342 CheckDefault( "tooltiptext", "" );
343 CheckDefault( "help", "" );
345 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
347 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
348 attr["lefttop"], attr["rightbottom"], attr["sequence"],
349 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
350 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
351 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
354 m_pData->m_listRadialSlider.push_back( radial );
357 else if( rName == "Slider" )
359 RequireDefault( "up" );
360 RequireDefault( "points" );
361 CheckDefault( "id", "none" );
362 CheckDefault( "visible", "true" );
363 CheckDefault( "x", "0" );
364 CheckDefault( "y", "0" );
365 CheckDefault( "width", "0" );
366 CheckDefault( "height", "0" );
367 CheckDefault( "lefttop", "lefttop" );
368 CheckDefault( "rightbottom", "lefttop" );
369 CheckDefault( "down", "none" );
370 CheckDefault( "over", "none" );
371 CheckDefault( "thickness", "10" );
372 CheckDefault( "value", "none" );
373 CheckDefault( "tooltiptext", "" );
374 CheckDefault( "help", "" );
376 string newValue = attr["value"];
377 if( m_curListId != "" )
379 // Slider associated to a list
380 newValue = "playlist.slider";
382 else if( m_curTreeId != "" )
384 // Slider associated to a tree
385 newValue = "playtree.slider";
387 const BuilderData::Slider slider( uniqueId( attr["id"] ),
388 attr["visible"], atoi( attr["x"] ) + m_xOffset,
389 atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
390 attr["rightbottom"], attr["up"], attr["down"],
391 attr["over"], attr["points"], atoi( attr["thickness"] ),
392 newValue, "none", 0, 0, 0, 0, attr["tooltiptext"],
393 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
395 m_pData->m_listSlider.push_back( slider );
398 else if( rName == "SliderBackground" )
400 RequireDefault( "image" );
401 CheckDefault( "nbhoriz", "1" );
402 CheckDefault( "nbvert", "1" );
403 CheckDefault( "padhoriz", "0" );
404 CheckDefault( "padvert", "0" );
406 // Retrieve the current slider data
407 BuilderData::Slider &slider = m_pData->m_listSlider.back();
409 slider.m_imageId = attr["image"];
410 slider.m_nbHoriz = atoi( attr["nbhoriz"] );
411 slider.m_nbVert = atoi( attr["nbvert"] );
412 slider.m_padHoriz = atoi( attr["padhoriz"] );
413 slider.m_padVert = atoi( attr["padvert"] );
416 else if( rName == "Text" )
418 RequireDefault( "font" );
419 CheckDefault( "id", "none" );
420 CheckDefault( "visible", "true" );
421 CheckDefault( "x", "0" );
422 CheckDefault( "y", "0" );
423 CheckDefault( "text", "" );
424 CheckDefault( "color", "#000000" );
425 CheckDefault( "scrolling", "auto" );
426 CheckDefault( "alignment", "left" );
427 CheckDefault( "width", "0" );
428 CheckDefault( "lefttop", "lefttop" );
429 CheckDefault( "rightbottom", "lefttop" );
430 CheckDefault( "help", "" );
432 const BuilderData::Text textData( uniqueId( attr["id"] ),
433 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
434 attr["visible"], attr["font"],
435 attr["text"], atoi( attr["width"] ),
436 attr["lefttop"], attr["rightbottom"],
437 convertColor( attr["color"] ),
438 attr["scrolling"], attr["alignment"],
439 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
441 m_pData->m_listText.push_back( textData );
444 else if( rName == "Theme" )
446 RequireDefault( "version" );
447 CheckDefault( "tooltipfont", "defaultfont" );
448 CheckDefault( "magnet", "15" );
449 CheckDefault( "alpha", "255" );
450 CheckDefault( "movealpha", "255" );
453 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
455 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
456 attr["version"], SKINS_DTD_VERSION );
460 const BuilderData::Theme theme( attr["tooltipfont"],
461 atoi( attr["magnet"] ),
462 convertInRange( attr["alpha"], 1, 255, "alpha" ),
463 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
464 m_pData->m_listTheme.push_back( theme );
467 else if( rName == "ThemeInfo" )
469 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
473 else if( rName == "Video" )
475 CheckDefault( "id", "none" );
476 CheckDefault( "visible", "true" );
477 CheckDefault( "x", "0" );
478 CheckDefault( "y", "0" );
479 CheckDefault( "width", "0" );
480 CheckDefault( "height", "0" );
481 CheckDefault( "lefttop", "lefttop" );
482 CheckDefault( "rightbottom", "lefttop" );
483 CheckDefault( "autoresize", "false" );
484 CheckDefault( "help", "" );
486 const BuilderData::Video videoData( uniqueId( attr["id"] ),
487 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
488 atoi( attr["width"] ), atoi( attr["height" ]),
489 attr["lefttop"], attr["rightbottom"],
490 attr["visible"], convertBoolean( attr["autoresize"] ),
491 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
493 m_pData->m_listVideo.push_back( videoData );
496 else if( rName == "Window" )
498 CheckDefault( "id", "none" );
499 CheckDefault( "visible", "true" );
500 CheckDefault( "x", "0" );
501 CheckDefault( "y", "0" );
502 CheckDefault( "dragdrop", "true" );
503 CheckDefault( "playondrop", "true" );
505 m_curWindowId = uniqueId( attr["id"] );
506 const BuilderData::Window window( m_curWindowId,
507 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
508 convertBoolean( attr["visible"] ),
509 convertBoolean( attr["dragdrop"] ),
510 convertBoolean( attr["playondrop"] ) );
511 m_pData->m_listWindow.push_back( window );
516 void SkinParser::handleEndElement( const string &rName )
518 if( rName == "Group" )
520 m_xOffset -= m_xOffsetList.back();
521 m_yOffset -= m_yOffsetList.back();
522 m_xOffsetList.pop_back();
523 m_yOffsetList.pop_back();
525 else if( rName == "Playlist" )
529 else if( rName == "Playtree" )
536 bool SkinParser::convertBoolean( const char *value ) const
538 return strcmp( value, "true" ) == 0;
542 int SkinParser::convertColor( const char *transcolor ) const
544 unsigned long iRed, iGreen, iBlue;
545 iRed = iGreen = iBlue = 0;
546 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
547 return ( iRed << 16 | iGreen << 8 | iBlue );
551 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
552 const string &rAttribute ) const
554 int intValue = atoi( value );
556 if( intValue < minValue )
558 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
559 "expected range [%i, %i], using %i instead",
560 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
563 else if( intValue > maxValue )
565 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
566 "expected range [%i, %i], using %i instead",
567 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
577 const string SkinParser::generateId() const
582 snprintf( genId, 4, "%i", i++ );
584 string base = "_ReservedId_" + (string)genId;
590 const string SkinParser::uniqueId( const string &id )
594 if( m_idSet.find( id ) != m_idSet.end() )
596 // The id was already used
599 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
601 newId = generateId();
605 // OK, this is a new id
609 // Add the id to the set
610 m_idSet.insert( newId );