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 );
295 else if( rName == "Playlist" )
297 RequireDefault( "id" );
298 RequireDefault( "font" );
299 CheckDefault( "visible", "true" );
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( "fgcolor", "#000000" );
308 CheckDefault( "playcolor", "#FF0000" );
309 CheckDefault( "bgcolor1", "#FFFFFF" );
310 CheckDefault( "bgcolor2", "#FFFFFF" );
311 CheckDefault( "selcolor", "#0000FF" );
312 CheckDefault( "help", "" );
314 m_curListId = uniqueId( attr["id"] );
315 const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
316 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
317 atoi( attr["width"]), atoi( attr["height"] ),
318 attr["lefttop"], attr["rightbottom"], attr["font"],
319 "playlist", attr["bgimage"], attr["fgcolor"],
320 attr["playcolor"], attr["bgcolor1"], attr["bgcolor2"],
321 attr["selcolor"], attr["help"],
322 m_curLayer, m_curWindowId, m_curLayoutId );
324 m_pData->m_listList.push_back( listData );
327 else if( rName == "Playtree" )
329 RequireDefault( "id" );
330 RequireDefault( "font" );
331 CheckDefault( "visible", "true" );
332 CheckDefault( "flat", "false" );
333 CheckDefault( "x", "0" );
334 CheckDefault( "y", "0" );
335 CheckDefault( "width", "0" );
336 CheckDefault( "height", "0" );
337 CheckDefault( "lefttop", "lefttop" );
338 CheckDefault( "rightbottom", "lefttop" );
339 CheckDefault( "bgimage", "none" );
340 CheckDefault( "itemimage", "none" );
341 CheckDefault( "openimage", "none" );
342 CheckDefault( "closedimage", "none" );
343 CheckDefault( "fgcolor", "#000000" );
344 CheckDefault( "playcolor", "#FF0000" );
345 CheckDefault( "bgcolor1", "#FFFFFF" );
346 CheckDefault( "bgcolor2", "#FFFFFF" );
347 CheckDefault( "selcolor", "#0000FF" );
348 CheckDefault( "help", "" );
350 m_curTreeId = uniqueId( attr["id"] );
351 const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
352 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
354 atoi( attr["width"]), atoi( attr["height"] ),
355 attr["lefttop"], attr["rightbottom"],
356 attr["font"], "playtree",
357 attr["bgimage"], attr["itemimage"],
358 attr["openimage"], attr["closedimage"],
359 attr["fgcolor"], attr["playcolor"],
360 attr["bgcolor1"], attr["bgcolor2"],
361 attr["selcolor"], attr["help"],
362 m_curLayer, m_curWindowId, m_curLayoutId );
364 m_pData->m_listTree.push_back( treeData );
367 else if( rName == "RadialSlider" )
369 RequireDefault( "sequence" );
370 RequireDefault( "nbimages" );
371 CheckDefault( "id", "none" );
372 CheckDefault( "visible", "true" );
373 CheckDefault( "x", "0" );
374 CheckDefault( "y", "0" );
375 CheckDefault( "lefttop", "lefttop" );
376 CheckDefault( "rightbottom", "lefttop" );
377 CheckDefault( "minangle", "0" );
378 CheckDefault( "maxangle", "360" );
379 CheckDefault( "value", "none" );
380 CheckDefault( "tooltiptext", "" );
381 CheckDefault( "help", "" );
383 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
385 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
386 attr["lefttop"], attr["rightbottom"], attr["sequence"],
387 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
388 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
389 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
392 m_pData->m_listRadialSlider.push_back( radial );
395 else if( rName == "Slider" )
397 RequireDefault( "up" );
398 RequireDefault( "points" );
399 CheckDefault( "id", "none" );
400 CheckDefault( "visible", "true" );
401 CheckDefault( "x", "0" );
402 CheckDefault( "y", "0" );
403 CheckDefault( "width", "0" );
404 CheckDefault( "height", "0" );
405 CheckDefault( "lefttop", "lefttop" );
406 CheckDefault( "rightbottom", "lefttop" );
407 CheckDefault( "down", "none" );
408 CheckDefault( "over", "none" );
409 CheckDefault( "thickness", "10" );
410 CheckDefault( "value", "none" );
411 CheckDefault( "tooltiptext", "" );
412 CheckDefault( "help", "" );
414 string newValue = attr["value"];
415 if( m_curListId != "" )
417 // Slider associated to a list
418 newValue = "playlist.slider";
420 else if( m_curTreeId != "" )
422 // Slider associated to a tree
423 newValue = "playtree.slider";
425 const BuilderData::Slider slider( uniqueId( attr["id"] ),
426 attr["visible"], atoi( attr["x"] ) + m_xOffset,
427 atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
428 attr["rightbottom"], attr["up"], attr["down"],
429 attr["over"], attr["points"], atoi( attr["thickness"] ),
430 newValue, "none", 0, 0, 0, 0, attr["tooltiptext"],
431 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
433 m_pData->m_listSlider.push_back( slider );
436 else if( rName == "SliderBackground" )
438 RequireDefault( "image" );
439 CheckDefault( "nbhoriz", "1" );
440 CheckDefault( "nbvert", "1" );
441 CheckDefault( "padhoriz", "0" );
442 CheckDefault( "padvert", "0" );
444 // Retrieve the current slider data
445 BuilderData::Slider &slider = m_pData->m_listSlider.back();
447 slider.m_imageId = attr["image"];
448 slider.m_nbHoriz = atoi( attr["nbhoriz"] );
449 slider.m_nbVert = atoi( attr["nbvert"] );
450 slider.m_padHoriz = atoi( attr["padhoriz"] );
451 slider.m_padVert = atoi( attr["padvert"] );
454 else if( rName == "Text" )
456 RequireDefault( "font" );
457 CheckDefault( "id", "none" );
458 CheckDefault( "visible", "true" );
459 CheckDefault( "x", "0" );
460 CheckDefault( "y", "0" );
461 CheckDefault( "text", "" );
462 CheckDefault( "color", "#000000" );
463 CheckDefault( "scrolling", "auto" );
464 CheckDefault( "alignment", "left" );
465 CheckDefault( "width", "0" );
466 CheckDefault( "lefttop", "lefttop" );
467 CheckDefault( "rightbottom", "lefttop" );
468 CheckDefault( "help", "" );
470 const BuilderData::Text textData( uniqueId( attr["id"] ),
471 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
472 attr["visible"], attr["font"],
473 attr["text"], atoi( attr["width"] ),
474 attr["lefttop"], attr["rightbottom"],
475 convertColor( attr["color"] ),
476 attr["scrolling"], attr["alignment"],
477 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
479 m_pData->m_listText.push_back( textData );
482 else if( rName == "Theme" )
484 RequireDefault( "version" );
485 CheckDefault( "tooltipfont", "defaultfont" );
486 CheckDefault( "magnet", "15" );
487 CheckDefault( "alpha", "255" );
488 CheckDefault( "movealpha", "255" );
491 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
493 msg_Err( getIntf(), "bad theme version : %s (you need version %s)",
494 attr["version"], SKINS_DTD_VERSION );
498 const BuilderData::Theme theme( attr["tooltipfont"],
499 atoi( attr["magnet"] ),
500 convertInRange( attr["alpha"], 1, 255, "alpha" ),
501 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
502 m_pData->m_listTheme.push_back( theme );
505 else if( rName == "ThemeInfo" )
507 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
511 else if( rName == "Video" )
513 CheckDefault( "id", "none" );
514 CheckDefault( "visible", "true" );
515 CheckDefault( "x", "0" );
516 CheckDefault( "y", "0" );
517 CheckDefault( "width", "0" );
518 CheckDefault( "height", "0" );
519 CheckDefault( "lefttop", "lefttop" );
520 CheckDefault( "rightbottom", "lefttop" );
521 CheckDefault( "autoresize", "false" );
522 CheckDefault( "help", "" );
524 const BuilderData::Video videoData( uniqueId( attr["id"] ),
525 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
526 atoi( attr["width"] ), atoi( attr["height" ]),
527 attr["lefttop"], attr["rightbottom"],
528 attr["visible"], convertBoolean( attr["autoresize"] ),
529 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
531 m_pData->m_listVideo.push_back( videoData );
534 else if( rName == "Window" )
536 CheckDefault( "id", "none" );
537 CheckDefault( "visible", "true" );
538 CheckDefault( "x", "0" );
539 CheckDefault( "y", "0" );
540 CheckDefault( "dragdrop", "true" );
541 CheckDefault( "playondrop", "true" );
543 m_curWindowId = uniqueId( attr["id"] );
544 const BuilderData::Window window( m_curWindowId,
545 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
546 convertBoolean( attr["visible"] ),
547 convertBoolean( attr["dragdrop"] ),
548 convertBoolean( attr["playondrop"] ) );
549 m_pData->m_listWindow.push_back( window );
554 void SkinParser::handleEndElement( const string &rName )
556 if( rName == "Group" )
558 m_xOffset -= m_xOffsetList.back();
559 m_yOffset -= m_yOffsetList.back();
560 m_xOffsetList.pop_back();
561 m_yOffsetList.pop_back();
563 else if( rName == "Playlist" )
567 else if( rName == "Playtree" )
571 else if( rName == "Popup" )
574 m_popupPosList.pop_back();
579 bool SkinParser::convertBoolean( const char *value ) const
581 return strcmp( value, "true" ) == 0;
585 int SkinParser::convertColor( const char *transcolor )
587 // TODO: move to the builder
588 unsigned long iRed, iGreen, iBlue;
589 iRed = iGreen = iBlue = 0;
590 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
591 return ( iRed << 16 | iGreen << 8 | iBlue );
595 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
596 const string &rAttribute ) const
598 int intValue = atoi( value );
600 if( intValue < minValue )
602 msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
603 "expected range [%i, %i], using %i instead",
604 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
607 else if( intValue > maxValue )
609 msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
610 "expected range [%i, %i], using %i instead",
611 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
621 const string SkinParser::generateId() const
626 snprintf( genId, 4, "%i", i++ );
628 string base = "_ReservedId_" + (string)genId;
634 const string SkinParser::uniqueId( const string &id )
638 if( m_idSet.find( id ) != m_idSet.end() )
640 // The id was already used
643 msg_Warn( getIntf(), "non-unique id: %s", id.c_str() );
645 newId = generateId();
649 // OK, this is a new id
653 // Add the id to the set
654 m_idSet.insert( newId );