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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "skin_parser.hpp"
25 #include "../src/os_factory.hpp"
26 #include "interpreter.hpp"
29 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName,
30 const string &rPath, bool useDTD, BuilderData *pData ):
31 XMLParser( pIntf, rFileName, useDTD ), m_path( rPath), m_pData(pData),
32 m_ownData(pData == NULL), m_xOffset( 0 ), m_yOffset( 0 )
34 // Make sure the data is allocated
37 m_pData = new BuilderData();
42 SkinParser::~SkinParser()
51 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
53 #define CheckDefault( a, b ) \
54 if( attr.find(a) == attr.end() ) attr[strdup(a)] = strdup(b);
55 #define RequireDefault( a ) \
56 if( attr.find(a) == attr.end() ) \
58 msg_Err( getIntf(), "bad theme (element: %s, missing attribute: %s)", \
60 m_errors = true; return; \
63 if( rName == "Include" )
65 RequireDefault( "file" );
67 OSFactory *pFactory = OSFactory::instance( getIntf() );
68 string fullPath = m_path + pFactory->getDirSeparator() + attr["file"];
69 msg_Dbg( getIntf(), "opening included XML file: %s", fullPath.c_str() );
70 // FIXME: We do not use the DTD to validate the included XML file,
71 // as the parser seems to dislike it otherwise...
72 SkinParser subParser( getIntf(), fullPath.c_str(), false, m_pData );
76 else if( rName == "IniFile" )
78 RequireDefault( "id" );
79 RequireDefault( "file" );
81 const BuilderData::IniFile iniFile( attr["id"],
83 m_pData->m_listIniFile.push_back( iniFile );
86 else if( rName == "Anchor" )
88 RequireDefault( "priority" );
89 CheckDefault( "x", "0" );
90 CheckDefault( "y", "0" );
91 CheckDefault( "points", "(0,0)" );
92 CheckDefault( "range", "10" );
94 const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
95 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
96 atoi( attr["priority"] ), attr["points"], m_curLayoutId );
97 m_pData->m_listAnchor.push_back( anchor );
100 else if( rName == "Bitmap" )
102 RequireDefault( "id" );
103 RequireDefault( "file" );
104 RequireDefault( "alphacolor" );
105 CheckDefault( "nbframes", "1" );
106 CheckDefault( "fps", "4" );
108 m_curBitmapId = uniqueId( attr["id"] );
109 const BuilderData::Bitmap bitmap( m_curBitmapId,
110 attr["file"], convertColor( attr["alphacolor"] ),
111 atoi( attr["nbframes"] ), atoi( attr["fps"] ) );
112 m_pData->m_listBitmap.push_back( bitmap );
115 else if( rName == "SubBitmap" )
117 RequireDefault( "id" );
118 RequireDefault( "x" );
119 RequireDefault( "y" );
120 RequireDefault( "width" );
121 RequireDefault( "height" );
122 CheckDefault( "nbframes", "1" );
123 CheckDefault( "fps", "4" );
125 const BuilderData::SubBitmap bitmap( attr["id"],
126 m_curBitmapId, atoi( attr["x"] ), atoi( attr["y"] ),
127 atoi( attr["width"] ), atoi( attr["height"] ),
128 atoi( attr["nbframes"] ), atoi( attr["fps"] ) );
129 m_pData->m_listSubBitmap.push_back( bitmap );
132 else if( rName == "BitmapFont" )
134 RequireDefault( "id" );
135 RequireDefault( "file" );
136 CheckDefault( "type", "digits" );
138 const BuilderData::BitmapFont font( attr["id"],
139 attr["file"], attr["type"] );
140 m_pData->m_listBitmapFont.push_back( font );
143 else if( rName == "PopupMenu" )
145 RequireDefault( "id" );
147 m_popupPosList.push_back(0);
148 m_curPopupId = uniqueId( attr["id"] );
149 const BuilderData::PopupMenu popup( m_curPopupId );
150 m_pData->m_listPopupMenu.push_back( popup );
153 else if( rName == "MenuItem" )
155 RequireDefault( "label" );
156 CheckDefault( "action", "none" );
158 const BuilderData::MenuItem item( attr["label"], attr["action"],
159 m_popupPosList.back(),
161 m_pData->m_listMenuItem.push_back( item );
162 m_popupPosList.back()++;
165 else if( rName == "MenuSeparator" )
167 const BuilderData::MenuSeparator sep( m_popupPosList.back(),
169 m_pData->m_listMenuSeparator.push_back( sep );
170 m_popupPosList.back()++;
173 else if( rName == "Button" )
175 RequireDefault( "up" );
176 CheckDefault( "id", "none" );
177 CheckDefault( "visible", "true" );
178 CheckDefault( "x", "0" );
179 CheckDefault( "y", "0" );
180 CheckDefault( "lefttop", "lefttop" );
181 CheckDefault( "rightbottom", "lefttop" );
182 CheckDefault( "down", "none" );
183 CheckDefault( "over", "none" );
184 CheckDefault( "action", "none" );
185 CheckDefault( "tooltiptext", "" );
186 CheckDefault( "help", "" );
188 const BuilderData::Button button( uniqueId( attr["id"] ),
189 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
190 attr["lefttop"], attr["rightbottom"], attr["visible"],
191 attr["up"], attr["down"], attr["over"], attr["action"],
192 attr["tooltiptext"], attr["help"],
193 m_curLayer, m_curWindowId, m_curLayoutId );
195 m_pData->m_listButton.push_back( button );
198 else if( rName == "Checkbox" )
200 RequireDefault( "up1" );
201 RequireDefault( "up2" );
202 RequireDefault( "state" );
203 CheckDefault( "id", "none" );
204 CheckDefault( "visible", "true" );
205 CheckDefault( "x", "0" );
206 CheckDefault( "y", "0" );
207 CheckDefault( "lefttop", "lefttop" );
208 CheckDefault( "rightbottom", "lefttop" );
209 CheckDefault( "down1", "none" );
210 CheckDefault( "over1", "none" );
211 CheckDefault( "down2", "none" );
212 CheckDefault( "over2", "none" );
213 CheckDefault( "action1", "none" );
214 CheckDefault( "action2", "none" );
215 CheckDefault( "tooltiptext1", "" );
216 CheckDefault( "tooltiptext2", "" );
217 CheckDefault( "help", "" );
219 const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
220 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
221 attr["lefttop"], attr["rightbottom"], attr["visible"],
222 attr["up1"], attr["down1"], attr["over1"],
223 attr["up2"], attr["down2"], attr["over2"], attr["state"],
224 attr["action1"], attr["action2"], attr["tooltiptext1"],
225 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
228 m_pData->m_listCheckbox.push_back( checkbox );
231 else if( rName == "Font" )
233 RequireDefault( "id" );
234 RequireDefault( "file" );
235 CheckDefault( "size", "12" );
237 const BuilderData::Font fontData( uniqueId( attr["id"] ),
238 attr["file"], atoi( attr["size"] ) );
239 m_pData->m_listFont.push_back( fontData );
242 else if( rName == "Group" )
244 CheckDefault( "x", "0" );
245 CheckDefault( "y", "0" );
247 m_xOffset += atoi( attr["x"] );
248 m_yOffset += atoi( attr["y"] );
249 m_xOffsetList.push_back( atoi( attr["x"] ) );
250 m_yOffsetList.push_back( atoi( attr["y"] ) );
253 else if( rName == "Image" )
255 RequireDefault( "image" );
256 CheckDefault( "id", "none" );
257 CheckDefault( "visible", "true" );
258 CheckDefault( "x", "0" );
259 CheckDefault( "y", "0" );
260 CheckDefault( "lefttop", "lefttop" );
261 CheckDefault( "rightbottom", "lefttop" );
262 CheckDefault( "action", "none" );
263 CheckDefault( "action2", "none" );
264 CheckDefault( "resize", "mosaic" );
265 CheckDefault( "help", "" );
267 const BuilderData::Image imageData( uniqueId( attr["id"] ),
268 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
269 attr["lefttop"], attr["rightbottom"], attr["visible"],
270 attr["image"], attr["action"], attr["action2"], attr["resize"],
271 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
273 m_pData->m_listImage.push_back( imageData );
276 else if( rName == "Layout" )
278 RequireDefault( "width" );
279 RequireDefault( "height" );
280 CheckDefault( "id", "none" );
281 CheckDefault( "minwidth", "-1" );
282 CheckDefault( "maxwidth", "-1" );
283 CheckDefault( "minheight", "-1" );
284 CheckDefault( "maxheight", "-1" );
286 m_curLayoutId = uniqueId( attr["id"] );
287 const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
288 atoi( attr["height"] ), atoi( attr["minwidth"] ),
289 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
290 atoi( attr["maxheight"] ), m_curWindowId );
291 m_pData->m_listLayout.push_back( layout );
294 else if( rName == "Playlist" )
296 RequireDefault( "id" );
297 RequireDefault( "font" );
298 CheckDefault( "visible", "true" );
299 CheckDefault( "flat", "true" ); // Only difference here
300 CheckDefault( "x", "0" );
301 CheckDefault( "y", "0" );
302 CheckDefault( "width", "0" );
303 CheckDefault( "height", "0" );
304 CheckDefault( "lefttop", "lefttop" );
305 CheckDefault( "rightbottom", "lefttop" );
306 CheckDefault( "bgimage", "none" );
307 CheckDefault( "itemimage", "none" );
308 CheckDefault( "openimage", "none" );
309 CheckDefault( "closedimage", "none" );
310 CheckDefault( "fgcolor", "#000000" );
311 CheckDefault( "playcolor", "#FF0000" );
312 CheckDefault( "bgcolor1", "#FFFFFF" );
313 CheckDefault( "bgcolor2", "#FFFFFF" );
314 CheckDefault( "selcolor", "#0000FF" );
315 CheckDefault( "help", "" );
316 m_curTreeId = uniqueId( attr["id"] );
317 const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
318 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
320 atoi( attr["width"]), atoi( attr["height"] ),
321 attr["lefttop"], attr["rightbottom"],
322 attr["font"], "playtree",
323 attr["bgimage"], attr["itemimage"],
324 attr["openimage"], attr["closedimage"],
329 attr["selcolor"], attr["help"],
330 m_curLayer, m_curWindowId, m_curLayoutId );
332 m_pData->m_listTree.push_back( treeData );
334 else if( rName == "Playtree" )
336 RequireDefault( "id" );
337 RequireDefault( "font" );
338 CheckDefault( "visible", "true" );
339 CheckDefault( "flat", "false" );
340 CheckDefault( "x", "0" );
341 CheckDefault( "y", "0" );
342 CheckDefault( "width", "0" );
343 CheckDefault( "height", "0" );
344 CheckDefault( "lefttop", "lefttop" );
345 CheckDefault( "rightbottom", "lefttop" );
346 CheckDefault( "bgimage", "none" );
347 CheckDefault( "itemimage", "none" );
348 CheckDefault( "openimage", "none" );
349 CheckDefault( "closedimage", "none" );
350 CheckDefault( "fgcolor", "#000000" );
351 CheckDefault( "playcolor", "#FF0000" );
352 CheckDefault( "bgcolor1", "#FFFFFF" );
353 CheckDefault( "bgcolor2", "#FFFFFF" );
354 CheckDefault( "selcolor", "#0000FF" );
355 CheckDefault( "help", "" );
357 m_curTreeId = uniqueId( attr["id"] );
358 const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
359 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
361 atoi( attr["width"]), atoi( attr["height"] ),
362 attr["lefttop"], attr["rightbottom"],
363 attr["font"], "playtree",
364 attr["bgimage"], attr["itemimage"],
365 attr["openimage"], attr["closedimage"],
366 attr["fgcolor"], attr["playcolor"],
367 attr["bgcolor1"], attr["bgcolor2"],
368 attr["selcolor"], attr["help"],
369 m_curLayer, m_curWindowId, m_curLayoutId );
371 m_pData->m_listTree.push_back( treeData );
374 else if( rName == "RadialSlider" )
376 RequireDefault( "sequence" );
377 RequireDefault( "nbimages" );
378 CheckDefault( "id", "none" );
379 CheckDefault( "visible", "true" );
380 CheckDefault( "x", "0" );
381 CheckDefault( "y", "0" );
382 CheckDefault( "lefttop", "lefttop" );
383 CheckDefault( "rightbottom", "lefttop" );
384 CheckDefault( "minangle", "0" );
385 CheckDefault( "maxangle", "360" );
386 CheckDefault( "value", "none" );
387 CheckDefault( "tooltiptext", "" );
388 CheckDefault( "help", "" );
390 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
392 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
393 attr["lefttop"], attr["rightbottom"], attr["sequence"],
394 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
395 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
396 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
399 m_pData->m_listRadialSlider.push_back( radial );
402 else if( rName == "Slider" )
404 RequireDefault( "up" );
405 RequireDefault( "points" );
406 CheckDefault( "id", "none" );
407 CheckDefault( "visible", "true" );
408 CheckDefault( "x", "0" );
409 CheckDefault( "y", "0" );
410 CheckDefault( "width", "0" );
411 CheckDefault( "height", "0" );
412 CheckDefault( "lefttop", "lefttop" );
413 CheckDefault( "rightbottom", "lefttop" );
414 CheckDefault( "down", "none" );
415 CheckDefault( "over", "none" );
416 CheckDefault( "thickness", "10" );
417 CheckDefault( "value", "none" );
418 CheckDefault( "tooltiptext", "" );
419 CheckDefault( "help", "" );
421 string newValue = attr["value"];
422 if( m_curTreeId != "" )
424 // Slider associated to a tree
425 newValue = "playtree.slider";
427 const BuilderData::Slider slider( uniqueId( attr["id"] ),
428 attr["visible"], atoi( attr["x"] ) + m_xOffset,
429 atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
430 attr["rightbottom"], attr["up"], attr["down"],
431 attr["over"], attr["points"], atoi( attr["thickness"] ),
432 newValue, "none", 0, 0, 0, 0, attr["tooltiptext"],
433 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
435 m_pData->m_listSlider.push_back( slider );
438 else if( rName == "SliderBackground" )
440 RequireDefault( "image" );
441 CheckDefault( "nbhoriz", "1" );
442 CheckDefault( "nbvert", "1" );
443 CheckDefault( "padhoriz", "0" );
444 CheckDefault( "padvert", "0" );
446 // Retrieve the current slider data
447 BuilderData::Slider &slider = m_pData->m_listSlider.back();
449 slider.m_imageId = attr["image"];
450 slider.m_nbHoriz = atoi( attr["nbhoriz"] );
451 slider.m_nbVert = atoi( attr["nbvert"] );
452 slider.m_padHoriz = atoi( attr["padhoriz"] );
453 slider.m_padVert = atoi( attr["padvert"] );
456 else if( rName == "Text" )
458 RequireDefault( "font" );
459 CheckDefault( "id", "none" );
460 CheckDefault( "visible", "true" );
461 CheckDefault( "x", "0" );
462 CheckDefault( "y", "0" );
463 CheckDefault( "text", "" );
464 CheckDefault( "color", "#000000" );
465 CheckDefault( "scrolling", "auto" );
466 CheckDefault( "alignment", "left" );
467 CheckDefault( "width", "0" );
468 CheckDefault( "lefttop", "lefttop" );
469 CheckDefault( "rightbottom", "lefttop" );
470 CheckDefault( "help", "" );
472 const BuilderData::Text textData( uniqueId( attr["id"] ),
473 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
474 attr["visible"], attr["font"],
475 attr["text"], atoi( attr["width"] ),
476 attr["lefttop"], attr["rightbottom"],
477 convertColor( attr["color"] ),
478 attr["scrolling"], attr["alignment"],
479 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
481 m_pData->m_listText.push_back( textData );
484 else if( rName == "Theme" )
486 RequireDefault( "version" );
487 CheckDefault( "tooltipfont", "defaultfont" );
488 CheckDefault( "magnet", "15" );
489 CheckDefault( "alpha", "255" );
490 CheckDefault( "movealpha", "255" );
493 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
495 msg_Err( getIntf(), "bad theme version : %s (you need version %s)",
496 attr["version"], SKINS_DTD_VERSION );
500 const BuilderData::Theme theme( attr["tooltipfont"],
501 atoi( attr["magnet"] ),
502 convertInRange( attr["alpha"], 1, 255, "alpha" ),
503 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
504 m_pData->m_listTheme.push_back( theme );
507 else if( rName == "ThemeInfo" )
509 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
513 else if( rName == "Video" )
515 CheckDefault( "id", "none" );
516 CheckDefault( "visible", "true" );
517 CheckDefault( "x", "0" );
518 CheckDefault( "y", "0" );
519 CheckDefault( "width", "0" );
520 CheckDefault( "height", "0" );
521 CheckDefault( "lefttop", "lefttop" );
522 CheckDefault( "rightbottom", "lefttop" );
523 CheckDefault( "autoresize", "false" );
524 CheckDefault( "help", "" );
526 const BuilderData::Video videoData( uniqueId( attr["id"] ),
527 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
528 atoi( attr["width"] ), atoi( attr["height" ]),
529 attr["lefttop"], attr["rightbottom"],
530 attr["visible"], convertBoolean( attr["autoresize"] ),
531 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
533 m_pData->m_listVideo.push_back( videoData );
536 else if( rName == "Window" )
538 CheckDefault( "id", "none" );
539 CheckDefault( "visible", "true" );
540 CheckDefault( "x", "0" );
541 CheckDefault( "y", "0" );
542 CheckDefault( "dragdrop", "true" );
543 CheckDefault( "playondrop", "true" );
545 m_curWindowId = uniqueId( attr["id"] );
546 const BuilderData::Window window( m_curWindowId,
547 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
548 convertBoolean( attr["visible"] ),
549 convertBoolean( attr["dragdrop"] ),
550 convertBoolean( attr["playondrop"] ) );
551 m_pData->m_listWindow.push_back( window );
556 void SkinParser::handleEndElement( const string &rName )
558 if( rName == "Group" )
560 m_xOffset -= m_xOffsetList.back();
561 m_yOffset -= m_yOffsetList.back();
562 m_xOffsetList.pop_back();
563 m_yOffsetList.pop_back();
565 else if( rName == "Playtree" || rName == "Playlist" )
569 else if( rName == "Popup" )
572 m_popupPosList.pop_back();
577 bool SkinParser::convertBoolean( const char *value ) const
579 return strcmp( value, "true" ) == 0;
583 int SkinParser::convertColor( const char *transcolor )
585 // TODO: move to the builder
586 unsigned long iRed, iGreen, iBlue;
587 iRed = iGreen = iBlue = 0;
588 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
589 return ( iRed << 16 | iGreen << 8 | iBlue );
593 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
594 const string &rAttribute ) const
596 int intValue = atoi( value );
598 if( intValue < minValue )
600 msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
601 "expected range [%i, %i], using %i instead",
602 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
605 else if( intValue > maxValue )
607 msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
608 "expected range [%i, %i], using %i instead",
609 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
619 const string SkinParser::generateId() const
624 snprintf( genId, 4, "%i", i++ );
626 string base = "_ReservedId_" + (string)genId;
632 const string SkinParser::uniqueId( const string &id )
636 if( m_idSet.find( id ) != m_idSet.end() )
638 // The id was already used
641 msg_Warn( getIntf(), "non-unique id: %s", id.c_str() );
643 newId = generateId();
647 // OK, this is a new id
651 // Add the id to the set
652 m_idSet.insert( newId );