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( 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 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( "background", "none" );
367 CheckDefault( "nbimages", "1" );
368 CheckDefault( "tooltiptext", "" );
369 CheckDefault( "help", "" );
371 string newValue = attr["value"];
372 if( m_curListId != "" )
374 // Slider associated to a list
375 newValue = "playlist.slider";
377 else if( m_curTreeId != "" )
379 // Slider associated to a tree
380 newValue = "playtree.slider";
382 const BuilderData::Slider slider( uniqueId( attr["id"] ),
384 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
385 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
386 attr["over"], attr["points"], atoi( attr["thickness"] ),
387 newValue, attr["background"], atoi( attr["nbimages"] ),
388 attr["tooltiptext"], attr["help"], m_curLayer,
389 m_curWindowId, m_curLayoutId );
391 m_pData->m_listSlider.push_back( slider );
394 else if( rName == "Text" )
396 RequireDefault( "font" );
397 CheckDefault( "id", "none" );
398 CheckDefault( "visible", "true" );
399 CheckDefault( "x", "0" );
400 CheckDefault( "y", "0" );
401 CheckDefault( "text", "" );
402 CheckDefault( "color", "#000000" );
403 CheckDefault( "width", "0" );
404 CheckDefault( "lefttop", "lefttop" );
405 CheckDefault( "rightbottom", "lefttop" );
406 CheckDefault( "help", "" );
408 const BuilderData::Text textData( uniqueId( attr["id"] ),
409 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
410 attr["visible"], attr["font"],
411 attr["text"], atoi( attr["width"] ),
412 attr["lefttop"], attr["rightbottom"],
413 convertColor( attr["color"] ), attr["help"], m_curLayer,
414 m_curWindowId, m_curLayoutId );
416 m_pData->m_listText.push_back( textData );
419 else if( rName == "Theme" )
421 RequireDefault( "version" );
422 CheckDefault( "tooltipfont", "defaultfont" );
423 CheckDefault( "magnet", "15" );
424 CheckDefault( "alpha", "255" );
425 CheckDefault( "movealpha", "255" );
428 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
430 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
431 attr["version"], SKINS_DTD_VERSION );
435 const BuilderData::Theme theme( attr["tooltipfont"],
436 atoi( attr["magnet"] ),
437 convertInRange( attr["alpha"], 1, 255, "alpha" ),
438 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
439 m_pData->m_listTheme.push_back( theme );
442 else if( rName == "ThemeInfo" )
444 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
448 else if( rName == "Video" )
450 CheckDefault( "id", "none" );
451 CheckDefault( "visible", "true" );
452 CheckDefault( "x", "0" );
453 CheckDefault( "y", "0" );
454 CheckDefault( "width", "0" );
455 CheckDefault( "height", "0" );
456 CheckDefault( "lefttop", "lefttop" );
457 CheckDefault( "rightbottom", "lefttop" );
458 CheckDefault( "autoresize", "false" );
459 CheckDefault( "help", "" );
461 const BuilderData::Video videoData( uniqueId( attr["id"] ),
462 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
463 atoi( attr["width"] ), atoi( attr["height" ]),
464 attr["lefttop"], attr["rightbottom"],
465 attr["visible"], convertBoolean( attr["autoresize"] ),
466 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
468 m_pData->m_listVideo.push_back( videoData );
471 else if( rName == "Window" )
473 CheckDefault( "id", "none" );
474 CheckDefault( "visible", "true" );
475 CheckDefault( "x", "0" );
476 CheckDefault( "y", "0" );
477 CheckDefault( "dragdrop", "true" );
478 CheckDefault( "playondrop", "true" );
480 m_curWindowId = uniqueId( attr["id"] );
481 const BuilderData::Window window( m_curWindowId,
482 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
483 convertBoolean( attr["visible"] ),
484 convertBoolean( attr["dragdrop"] ),
485 convertBoolean( attr["playondrop"] ) );
486 m_pData->m_listWindow.push_back( window );
491 void SkinParser::handleEndElement( const string &rName )
493 if( rName == "Group" )
495 m_xOffset -= m_xOffsetList.back();
496 m_yOffset -= m_yOffsetList.back();
497 m_xOffsetList.pop_back();
498 m_yOffsetList.pop_back();
500 else if( rName == "Playlist" )
504 else if( rName == "Playtree" )
511 bool SkinParser::convertBoolean( const char *value ) const
513 return strcmp( value, "true" ) == 0;
517 int SkinParser::convertColor( const char *transcolor ) const
519 unsigned long iRed, iGreen, iBlue;
520 iRed = iGreen = iBlue = 0;
521 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
522 return ( iRed << 16 | iGreen << 8 | iBlue );
526 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
527 const string &rAttribute ) const
529 int intValue = atoi( value );
531 if( intValue < minValue )
533 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
534 "expected range [%i, %i], using %i instead",
535 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
538 else if( intValue > maxValue )
540 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
541 "expected range [%i, %i], using %i instead",
542 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
552 const string SkinParser::generateId() const
557 snprintf( genId, 4, "%i", i++ );
559 string base = "_ReservedId_" + (string)genId;
565 const string SkinParser::uniqueId( const string &id )
569 if( m_idSet.find( id ) != m_idSet.end() )
571 // The id was already used
574 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
576 newId = generateId();
580 // OK, this is a new id
584 // Add the id to the set
585 m_idSet.insert( newId );