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" );
95 m_curBitmapId = uniqueId( attr["id"] );
96 const BuilderData::Bitmap bitmap( m_curBitmapId,
97 attr["file"], 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( 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 attr["file"], attr["type"] );
123 m_pData->m_listBitmapFont.push_back( font );
126 else if( rName == "Button" )
128 RequireDefault( "up" );
129 CheckDefault( "id", "none" );
130 CheckDefault( "visible", "true" );
131 CheckDefault( "x", "0" );
132 CheckDefault( "y", "0" );
133 CheckDefault( "lefttop", "lefttop" );
134 CheckDefault( "rightbottom", "lefttop" );
135 CheckDefault( "down", "none" );
136 CheckDefault( "over", "none" );
137 CheckDefault( "action", "none" );
138 CheckDefault( "tooltiptext", "" );
139 CheckDefault( "help", "" );
141 const BuilderData::Button button( uniqueId( attr["id"] ),
142 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
143 attr["lefttop"], attr["rightbottom"], attr["visible"],
144 attr["up"], attr["down"], attr["over"], attr["action"],
145 attr["tooltiptext"], attr["help"],
146 m_curLayer, m_curWindowId, m_curLayoutId );
148 m_pData->m_listButton.push_back( button );
151 else if( rName == "Checkbox" )
153 RequireDefault( "up1" );
154 RequireDefault( "up2" );
155 RequireDefault( "state" );
156 CheckDefault( "id", "none" );
157 CheckDefault( "visible", "true" );
158 CheckDefault( "x", "0" );
159 CheckDefault( "y", "0" );
160 CheckDefault( "lefttop", "lefttop" );
161 CheckDefault( "rightbottom", "lefttop" );
162 CheckDefault( "down1", "none" );
163 CheckDefault( "over1", "none" );
164 CheckDefault( "down2", "none" );
165 CheckDefault( "over2", "none" );
166 CheckDefault( "action1", "none" );
167 CheckDefault( "action2", "none" );
168 CheckDefault( "tooltiptext1", "" );
169 CheckDefault( "tooltiptext2", "" );
170 CheckDefault( "help", "" );
172 const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
173 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
174 attr["lefttop"], attr["rightbottom"], attr["visible"],
175 attr["up1"], attr["down1"], attr["over1"],
176 attr["up2"], attr["down2"], attr["over2"], attr["state"],
177 attr["action1"], attr["action2"], attr["tooltiptext1"],
178 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
181 m_pData->m_listCheckbox.push_back( checkbox );
184 else if( rName == "Font" )
186 RequireDefault( "id" );
187 RequireDefault( "file" );
188 CheckDefault( "size", "12" );
190 const BuilderData::Font fontData( uniqueId( attr["id"] ),
191 attr["file"], atoi( attr["size"] ) );
192 m_pData->m_listFont.push_back( fontData );
195 else if( rName == "Group" )
197 CheckDefault( "x", "0" );
198 CheckDefault( "y", "0" );
200 m_xOffset += atoi( attr["x"] );
201 m_yOffset += atoi( attr["y"] );
202 m_xOffsetList.push_back( atoi( attr["x"] ) );
203 m_yOffsetList.push_back( atoi( attr["y"] ) );
206 else if( rName == "Image" )
208 RequireDefault( "image" );
209 CheckDefault( "id", "none" );
210 CheckDefault( "visible", "true" );
211 CheckDefault( "x", "0" );
212 CheckDefault( "y", "0" );
213 CheckDefault( "lefttop", "lefttop" );
214 CheckDefault( "rightbottom", "lefttop" );
215 CheckDefault( "action", "none" );
216 CheckDefault( "resize", "mosaic" );
217 CheckDefault( "help", "" );
219 const BuilderData::Image imageData( uniqueId( attr["id"] ),
220 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
221 attr["lefttop"], attr["rightbottom"], attr["visible"],
222 attr["image"], attr["action"], attr["resize"], attr["help"],
223 m_curLayer, m_curWindowId, m_curLayoutId );
225 m_pData->m_listImage.push_back( imageData );
228 else if( rName == "Layout" )
230 RequireDefault( "width" );
231 RequireDefault( "height" );
232 CheckDefault( "id", "none" );
233 CheckDefault( "minwidth", "-1" );
234 CheckDefault( "maxwidth", "-1" );
235 CheckDefault( "minheight", "-1" );
236 CheckDefault( "maxheight", "-1" );
238 m_curLayoutId = uniqueId( attr["id"] );
239 const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
240 atoi( attr["height"] ), atoi( attr["minwidth"] ),
241 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
242 atoi( attr["maxheight"] ), m_curWindowId );
243 m_pData->m_listLayout.push_back( layout );
247 else if( rName == "Playlist" )
249 RequireDefault( "id" );
250 RequireDefault( "font" );
251 CheckDefault( "visible", "true" );
252 CheckDefault( "x", "0" );
253 CheckDefault( "y", "0" );
254 CheckDefault( "width", "0" );
255 CheckDefault( "height", "0" );
256 CheckDefault( "lefttop", "lefttop" );
257 CheckDefault( "rightbottom", "lefttop" );
258 CheckDefault( "bgimage", "none" );
259 CheckDefault( "fgcolor", "#000000" );
260 CheckDefault( "playcolor", "#FF0000" );
261 CheckDefault( "bgcolor1", "#FFFFFF" );
262 CheckDefault( "bgcolor2", "#FFFFFF" );
263 CheckDefault( "selcolor", "#0000FF" );
264 CheckDefault( "help", "" );
266 m_curListId = uniqueId( attr["id"] );
267 const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
268 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
269 atoi( attr["width"]), atoi( attr["height"] ),
270 attr["lefttop"], attr["rightbottom"],
271 attr["font"], "playlist", attr["bgimage"],
272 convertColor( attr["fgcolor"] ),
273 convertColor( attr["playcolor"] ),
274 convertColor( attr["bgcolor1"] ),
275 convertColor( attr["bgcolor2"] ),
276 convertColor( attr["selcolor"] ), attr["help"],
277 m_curLayer, m_curWindowId, m_curLayoutId );
279 m_pData->m_listList.push_back( listData );
282 else if( rName == "Playtree" )
284 RequireDefault( "id" );
285 RequireDefault( "font" );
286 CheckDefault( "visible", "true" );
287 CheckDefault( "x", "0" );
288 CheckDefault( "y", "0" );
289 CheckDefault( "width", "0" );
290 CheckDefault( "height", "0" );
291 CheckDefault( "lefttop", "lefttop" );
292 CheckDefault( "rightbottom", "lefttop" );
293 CheckDefault( "bgimage", "none" );
294 CheckDefault( "itemimage", "none" );
295 CheckDefault( "openimage", "none" );
296 CheckDefault( "closedimage", "none" );
297 CheckDefault( "fgcolor", "#000000" );
298 CheckDefault( "playcolor", "#FF0000" );
299 CheckDefault( "bgcolor1", "#FFFFFF" );
300 CheckDefault( "bgcolor2", "#FFFFFF" );
301 CheckDefault( "selcolor", "#0000FF" );
302 CheckDefault( "help", "" );
304 m_curTreeId = uniqueId( attr["id"] );
305 const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
306 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
307 atoi( attr["width"]), atoi( attr["height"] ),
308 attr["lefttop"], attr["rightbottom"],
309 attr["font"], "playtree",
310 attr["bgimage"], attr["itemimage"],
311 attr["openimage"], attr["closedimage"],
312 convertColor( attr["fgcolor"] ),
313 convertColor( attr["playcolor"] ),
314 convertColor( attr["bgcolor1"] ),
315 convertColor( attr["bgcolor2"] ),
316 convertColor( attr["selcolor"] ), attr["help"],
317 m_curLayer, m_curWindowId, m_curLayoutId );
319 m_pData->m_listTree.push_back( treeData );
322 else if( rName == "RadialSlider" )
324 RequireDefault( "sequence" );
325 RequireDefault( "nbimages" );
326 CheckDefault( "id", "none" );
327 CheckDefault( "visible", "true" );
328 CheckDefault( "x", "0" );
329 CheckDefault( "y", "0" );
330 CheckDefault( "lefttop", "lefttop" );
331 CheckDefault( "rightbottom", "lefttop" );
332 CheckDefault( "minangle", "0" );
333 CheckDefault( "maxangle", "360" );
334 CheckDefault( "value", "none" );
335 CheckDefault( "tooltiptext", "" );
336 CheckDefault( "help", "" );
338 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
340 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
341 attr["lefttop"], attr["rightbottom"], attr["sequence"],
342 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
343 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
344 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
347 m_pData->m_listRadialSlider.push_back( radial );
350 else if( rName == "Slider" )
352 RequireDefault( "up" );
353 RequireDefault( "points" );
354 CheckDefault( "id", "none" );
355 CheckDefault( "visible", "true" );
356 CheckDefault( "x", "0" );
357 CheckDefault( "y", "0" );
358 CheckDefault( "width", "0" );
359 CheckDefault( "height", "0" );
360 CheckDefault( "lefttop", "lefttop" );
361 CheckDefault( "rightbottom", "lefttop" );
362 CheckDefault( "down", "none" );
363 CheckDefault( "over", "none" );
364 CheckDefault( "thickness", "10" );
365 CheckDefault( "value", "none" );
366 CheckDefault( "tooltiptext", "" );
367 CheckDefault( "help", "" );
369 string newValue = attr["value"];
370 if( m_curListId != "" )
372 // Slider associated to a list
373 newValue = "playlist.slider";
375 else if( m_curTreeId != "" )
377 // Slider associated to a tree
378 newValue = "playtree.slider";
380 const BuilderData::Slider slider( uniqueId( attr["id"] ),
381 attr["visible"], atoi( attr["x"] ) + m_xOffset,
382 atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
383 attr["rightbottom"], attr["up"], attr["down"],
384 attr["over"], attr["points"], atoi( attr["thickness"] ),
385 newValue, "none", 0, 0, 0, 0, attr["tooltiptext"],
386 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
388 m_pData->m_listSlider.push_back( slider );
391 else if( rName == "SliderBackground" )
393 RequireDefault( "image" );
394 CheckDefault( "nbhoriz", "1" );
395 CheckDefault( "nbvert", "1" );
396 CheckDefault( "padhoriz", "0" );
397 CheckDefault( "padvert", "0" );
399 // Retrieve the current slider data
400 BuilderData::Slider &slider = m_pData->m_listSlider.back();
402 slider.m_imageId = attr["image"];
403 slider.m_nbHoriz = atoi( attr["nbhoriz"] );
404 slider.m_nbVert = atoi( attr["nbvert"] );
405 slider.m_padHoriz = atoi( attr["padhoriz"] );
406 slider.m_padVert = atoi( attr["padvert"] );
409 else if( rName == "Text" )
411 RequireDefault( "font" );
412 CheckDefault( "id", "none" );
413 CheckDefault( "visible", "true" );
414 CheckDefault( "x", "0" );
415 CheckDefault( "y", "0" );
416 CheckDefault( "text", "" );
417 CheckDefault( "color", "#000000" );
418 CheckDefault( "width", "0" );
419 CheckDefault( "lefttop", "lefttop" );
420 CheckDefault( "rightbottom", "lefttop" );
421 CheckDefault( "help", "" );
423 const BuilderData::Text textData( uniqueId( attr["id"] ),
424 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
425 attr["visible"], attr["font"],
426 attr["text"], atoi( attr["width"] ),
427 attr["lefttop"], attr["rightbottom"],
428 convertColor( attr["color"] ), attr["help"], m_curLayer,
429 m_curWindowId, m_curLayoutId );
431 m_pData->m_listText.push_back( textData );
434 else if( rName == "Theme" )
436 RequireDefault( "version" );
437 CheckDefault( "tooltipfont", "defaultfont" );
438 CheckDefault( "magnet", "15" );
439 CheckDefault( "alpha", "255" );
440 CheckDefault( "movealpha", "255" );
443 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
445 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
446 attr["version"], SKINS_DTD_VERSION );
450 const BuilderData::Theme theme( attr["tooltipfont"],
451 atoi( attr["magnet"] ),
452 convertInRange( attr["alpha"], 1, 255, "alpha" ),
453 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
454 m_pData->m_listTheme.push_back( theme );
457 else if( rName == "ThemeInfo" )
459 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
463 else if( rName == "Video" )
465 CheckDefault( "id", "none" );
466 CheckDefault( "visible", "true" );
467 CheckDefault( "x", "0" );
468 CheckDefault( "y", "0" );
469 CheckDefault( "width", "0" );
470 CheckDefault( "height", "0" );
471 CheckDefault( "lefttop", "lefttop" );
472 CheckDefault( "rightbottom", "lefttop" );
473 CheckDefault( "autoresize", "false" );
474 CheckDefault( "help", "" );
476 const BuilderData::Video videoData( uniqueId( attr["id"] ),
477 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
478 atoi( attr["width"] ), atoi( attr["height" ]),
479 attr["lefttop"], attr["rightbottom"],
480 attr["visible"], convertBoolean( attr["autoresize"] ),
481 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
483 m_pData->m_listVideo.push_back( videoData );
486 else if( rName == "Window" )
488 CheckDefault( "id", "none" );
489 CheckDefault( "visible", "true" );
490 CheckDefault( "x", "0" );
491 CheckDefault( "y", "0" );
492 CheckDefault( "dragdrop", "true" );
493 CheckDefault( "playondrop", "true" );
495 m_curWindowId = uniqueId( attr["id"] );
496 const BuilderData::Window window( m_curWindowId,
497 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
498 convertBoolean( attr["visible"] ),
499 convertBoolean( attr["dragdrop"] ),
500 convertBoolean( attr["playondrop"] ) );
501 m_pData->m_listWindow.push_back( window );
506 void SkinParser::handleEndElement( const string &rName )
508 if( rName == "Group" )
510 m_xOffset -= m_xOffsetList.back();
511 m_yOffset -= m_yOffsetList.back();
512 m_xOffsetList.pop_back();
513 m_yOffsetList.pop_back();
515 else if( rName == "Playlist" )
519 else if( rName == "Playtree" )
526 bool SkinParser::convertBoolean( const char *value ) const
528 return strcmp( value, "true" ) == 0;
532 int SkinParser::convertColor( const char *transcolor ) const
534 unsigned long iRed, iGreen, iBlue;
535 iRed = iGreen = iBlue = 0;
536 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
537 return ( iRed << 16 | iGreen << 8 | iBlue );
541 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
542 const string &rAttribute ) const
544 int intValue = atoi( value );
546 if( intValue < minValue )
548 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
549 "expected range [%i, %i], using %i instead",
550 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
553 else if( intValue > maxValue )
555 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
556 "expected range [%i, %i], using %i instead",
557 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
567 const string SkinParser::generateId() const
572 snprintf( genId, 4, "%i", i++ );
574 string base = "_ReservedId_" + (string)genId;
580 const string SkinParser::uniqueId( const string &id )
584 if( m_idSet.find( id ) != m_idSet.end() )
586 // The id was already used
589 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
591 newId = generateId();
595 // OK, this is a new id
599 // Add the id to the set
600 m_idSet.insert( newId );