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 ):
30 XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 ),
36 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
38 #define CheckDefault( a, b ) \
39 if( attr.find(a) == attr.end() ) attr[strdup(a)] = strdup(b);
40 #define RequireDefault( a ) \
41 if( attr.find(a) == attr.end() ) \
43 msg_Err( getIntf(), "Bad theme (element: %s, missing attribute: %s)", \
45 m_errors = true; return; \
48 if( rName == "Anchor" )
50 RequireDefault( "priority" );
51 CheckDefault( "x", "0" );
52 CheckDefault( "y", "0" );
53 CheckDefault( "points", "(0,0)" );
54 CheckDefault( "range", "10" );
56 const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
57 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
58 atoi( attr["priority"] ), attr["points"], m_curLayoutId );
59 m_data.m_listAnchor.push_back( anchor );
62 else if( rName == "Bitmap" )
64 RequireDefault( "id" );
65 RequireDefault( "file" );
66 RequireDefault( "alphacolor" );
68 m_curBitmapId = uniqueId( attr["id"] );
69 const BuilderData::Bitmap bitmap( m_curBitmapId,
70 convertFileName( attr["file"] ),
71 convertColor( attr["alphacolor"] ) );
72 m_data.m_listBitmap.push_back( bitmap );
75 else if( rName == "SubBitmap" )
77 RequireDefault( "id" );
78 RequireDefault( "x" );
79 RequireDefault( "y" );
80 RequireDefault( "width" );
81 RequireDefault( "height" );
83 const BuilderData::SubBitmap bitmap( uniqueId( attr["id"] ),
84 m_curBitmapId, atoi( attr["x"] ), atoi( attr["y"] ),
85 atoi( attr["width"] ), atoi( attr["height"] ) );
86 m_data.m_listSubBitmap.push_back( bitmap );
89 else if( rName == "BitmapFont" )
91 RequireDefault( "id" );
92 RequireDefault( "file" );
93 CheckDefault( "type", "digits" );
95 const BuilderData::BitmapFont font( uniqueId( attr["id"] ),
96 convertFileName( attr["file"] ),
98 m_data.m_listBitmapFont.push_back( font );
101 else if( rName == "Button" )
103 RequireDefault( "up" );
104 CheckDefault( "id", "none" );
105 CheckDefault( "visible", "true" );
106 CheckDefault( "x", "0" );
107 CheckDefault( "y", "0" );
108 CheckDefault( "lefttop", "lefttop" );
109 CheckDefault( "rightbottom", "lefttop" );
110 CheckDefault( "down", "none" );
111 CheckDefault( "over", "none" );
112 CheckDefault( "action", "none" );
113 CheckDefault( "tooltiptext", "" );
114 CheckDefault( "help", "" );
116 const BuilderData::Button button( uniqueId( attr["id"] ),
117 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
118 attr["lefttop"], attr["rightbottom"], attr["visible"],
119 attr["up"], attr["down"], attr["over"], attr["action"],
120 attr["tooltiptext"], attr["help"],
121 m_curLayer, m_curWindowId, m_curLayoutId );
123 m_data.m_listButton.push_back( button );
126 else if( rName == "Checkbox" )
128 RequireDefault( "up1" );
129 RequireDefault( "up2" );
130 RequireDefault( "state" );
131 CheckDefault( "id", "none" );
132 CheckDefault( "visible", "true" );
133 CheckDefault( "x", "0" );
134 CheckDefault( "y", "0" );
135 CheckDefault( "lefttop", "lefttop" );
136 CheckDefault( "rightbottom", "lefttop" );
137 CheckDefault( "down1", "none" );
138 CheckDefault( "over1", "none" );
139 CheckDefault( "down2", "none" );
140 CheckDefault( "over2", "none" );
141 CheckDefault( "action1", "none" );
142 CheckDefault( "action2", "none" );
143 CheckDefault( "tooltiptext1", "" );
144 CheckDefault( "tooltiptext2", "" );
145 CheckDefault( "help", "" );
147 const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
148 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
149 attr["lefttop"], attr["rightbottom"], attr["visible"],
150 attr["up1"], attr["down1"], attr["over1"],
151 attr["up2"], attr["down2"], attr["over2"], attr["state"],
152 attr["action1"], attr["action2"], attr["tooltiptext1"],
153 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
156 m_data.m_listCheckbox.push_back( checkbox );
159 else if( rName == "Font" )
161 RequireDefault( "id" );
162 RequireDefault( "file" );
163 CheckDefault( "size", "12" );
165 const BuilderData::Font fontData( uniqueId( attr["id"] ),
166 convertFileName( attr["file"] ),
167 atoi( attr["size"] ) );
168 m_data.m_listFont.push_back( fontData );
171 else if( rName == "Group" )
173 CheckDefault( "x", "0" );
174 CheckDefault( "y", "0" );
176 m_xOffset += atoi( attr["x"] );
177 m_yOffset += atoi( attr["y"] );
178 m_xOffsetList.push_back( atoi( attr["x"] ) );
179 m_yOffsetList.push_back( atoi( attr["y"] ) );
182 else if( rName == "Image" )
184 RequireDefault( "image" );
185 CheckDefault( "id", "none" );
186 CheckDefault( "visible", "true" );
187 CheckDefault( "x", "0" );
188 CheckDefault( "y", "0" );
189 CheckDefault( "lefttop", "lefttop" );
190 CheckDefault( "rightbottom", "lefttop" );
191 CheckDefault( "action", "none" );
192 CheckDefault( "resize", "mosaic" );
193 CheckDefault( "help", "" );
195 const BuilderData::Image imageData( uniqueId( attr["id"] ),
196 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
197 attr["lefttop"], attr["rightbottom"], attr["visible"],
198 attr["image"], attr["action"], attr["resize"], attr["help"],
199 m_curLayer, m_curWindowId, m_curLayoutId );
201 m_data.m_listImage.push_back( imageData );
204 else if( rName == "Layout" )
206 RequireDefault( "width" );
207 RequireDefault( "height" );
208 CheckDefault( "id", "none" );
209 CheckDefault( "minwidth", "-1" );
210 CheckDefault( "maxwidth", "-1" );
211 CheckDefault( "minheight", "-1" );
212 CheckDefault( "maxheight", "-1" );
214 m_curLayoutId = uniqueId( attr["id"] );
215 const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
216 atoi( attr["height"] ), atoi( attr["minwidth"] ),
217 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
218 atoi( attr["maxheight"] ), m_curWindowId );
219 m_data.m_listLayout.push_back( layout );
223 else if( rName == "Playlist" )
225 RequireDefault( "id" );
226 RequireDefault( "font" );
227 CheckDefault( "visible", "true" );
228 CheckDefault( "x", "0" );
229 CheckDefault( "y", "0" );
230 CheckDefault( "width", "0" );
231 CheckDefault( "height", "0" );
232 CheckDefault( "lefttop", "lefttop" );
233 CheckDefault( "rightbottom", "lefttop" );
234 CheckDefault( "bgimage", "none" );
235 CheckDefault( "fgcolor", "#000000" );
236 CheckDefault( "playcolor", "#FF0000" );
237 CheckDefault( "bgcolor1", "#FFFFFF" );
238 CheckDefault( "bgcolor2", "#FFFFFF" );
239 CheckDefault( "selcolor", "#0000FF" );
240 CheckDefault( "help", "" );
242 m_curListId = uniqueId( attr["id"] );
243 const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
244 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
245 atoi( attr["width"]), atoi( attr["height"] ),
246 attr["lefttop"], attr["rightbottom"],
247 attr["font"], "playlist", attr["bgimage"],
248 convertColor( attr["fgcolor"] ),
249 convertColor( attr["playcolor"] ),
250 convertColor( attr["bgcolor1"] ),
251 convertColor( attr["bgcolor2"] ),
252 convertColor( attr["selcolor"] ), attr["help"],
253 m_curLayer, m_curWindowId, m_curLayoutId );
255 m_data.m_listList.push_back( listData );
258 else if( rName == "Playtree" )
260 RequireDefault( "id" );
261 RequireDefault( "font" );
262 CheckDefault( "visible", "true" );
263 CheckDefault( "x", "0" );
264 CheckDefault( "y", "0" );
265 CheckDefault( "width", "0" );
266 CheckDefault( "height", "0" );
267 CheckDefault( "lefttop", "lefttop" );
268 CheckDefault( "rightbottom", "lefttop" );
269 CheckDefault( "bgimage", "none" );
270 CheckDefault( "itemimage", "none" );
271 CheckDefault( "openimage", "none" );
272 CheckDefault( "closedimage", "none" );
273 CheckDefault( "fgcolor", "#000000" );
274 CheckDefault( "playcolor", "#FF0000" );
275 CheckDefault( "bgcolor1", "#FFFFFF" );
276 CheckDefault( "bgcolor2", "#FFFFFF" );
277 CheckDefault( "selcolor", "#0000FF" );
278 CheckDefault( "help", "" );
280 m_curTreeId = uniqueId( attr["id"] );
281 const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
282 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
283 atoi( attr["width"]), atoi( attr["height"] ),
284 attr["lefttop"], attr["rightbottom"],
285 attr["font"], "playtree",
286 attr["bgimage"], attr["itemimage"],
287 attr["openimage"], attr["closedimage"],
288 convertColor( attr["fgcolor"] ),
289 convertColor( attr["playcolor"] ),
290 convertColor( attr["bgcolor1"] ),
291 convertColor( attr["bgcolor2"] ),
292 convertColor( attr["selcolor"] ), attr["help"],
293 m_curLayer, m_curWindowId, m_curLayoutId );
295 m_data.m_listTree.push_back( treeData );
298 else if( rName == "RadialSlider" )
300 RequireDefault( "sequence" );
301 RequireDefault( "nbimages" );
302 CheckDefault( "id", "none" );
303 CheckDefault( "visible", "true" );
304 CheckDefault( "x", "0" );
305 CheckDefault( "y", "0" );
306 CheckDefault( "lefttop", "lefttop" );
307 CheckDefault( "rightbottom", "lefttop" );
308 CheckDefault( "minangle", "0" );
309 CheckDefault( "maxangle", "360" );
310 CheckDefault( "value", "none" );
311 CheckDefault( "tooltiptext", "" );
312 CheckDefault( "help", "" );
314 const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
316 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
317 attr["lefttop"], attr["rightbottom"], attr["sequence"],
318 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
319 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
320 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
323 m_data.m_listRadialSlider.push_back( radial );
326 else if( rName == "Slider" )
328 RequireDefault( "up" );
329 RequireDefault( "points" );
330 CheckDefault( "id", "none" );
331 CheckDefault( "visible", "true" );
332 CheckDefault( "x", "0" );
333 CheckDefault( "y", "0" );
334 CheckDefault( "width", "0" );
335 CheckDefault( "height", "0" );
336 CheckDefault( "lefttop", "lefttop" );
337 CheckDefault( "rightbottom", "lefttop" );
338 CheckDefault( "down", "none" );
339 CheckDefault( "over", "none" );
340 CheckDefault( "thickness", "10" );
341 CheckDefault( "value", "none" );
342 CheckDefault( "tooltiptext", "" );
343 CheckDefault( "help", "" );
345 string newValue = attr["value"];
346 if( m_curListId != "" )
348 // Slider associated to a list
349 newValue = "playlist.slider";
351 else if( m_curTreeId != "" )
353 // Slider associated to a tree
354 newValue = "playtree.slider";
356 const BuilderData::Slider slider( uniqueId( attr["id"] ),
358 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
359 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
360 attr["over"], attr["points"], atoi( attr["thickness"] ),
361 newValue, attr["tooltiptext"], attr["help"], m_curLayer,
362 m_curWindowId, m_curLayoutId );
364 m_data.m_listSlider.push_back( slider );
367 else if( rName == "Text" )
369 RequireDefault( "font" );
370 CheckDefault( "id", "none" );
371 CheckDefault( "visible", "true" );
372 CheckDefault( "x", "0" );
373 CheckDefault( "y", "0" );
374 CheckDefault( "text", "" );
375 CheckDefault( "color", "#000000" );
376 CheckDefault( "width", "0" );
377 CheckDefault( "lefttop", "lefttop" );
378 CheckDefault( "rightbottom", "lefttop" );
379 CheckDefault( "help", "" );
381 const BuilderData::Text textData( uniqueId( attr["id"] ),
382 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
383 attr["visible"], attr["font"],
384 attr["text"], atoi( attr["width"] ),
385 attr["lefttop"], attr["rightbottom"],
386 convertColor( attr["color"] ), attr["help"], m_curLayer,
387 m_curWindowId, m_curLayoutId );
389 m_data.m_listText.push_back( textData );
392 else if( rName == "Theme" )
394 RequireDefault( "version" );
395 CheckDefault( "tooltipfont", "defaultfont" );
396 CheckDefault( "magnet", "15" );
397 CheckDefault( "alpha", "255" );
398 CheckDefault( "movealpha", "255" );
401 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
403 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
404 attr["version"], SKINS_DTD_VERSION );
408 const BuilderData::Theme theme( attr["tooltipfont"],
409 atoi( attr["magnet"] ),
410 convertInRange( attr["alpha"], 1, 255, "alpha" ),
411 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
412 m_data.m_listTheme.push_back( theme );
415 else if( rName == "ThemeInfo" )
417 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
421 else if( rName == "Video" )
423 CheckDefault( "id", "none" );
424 CheckDefault( "visible", "true" );
425 CheckDefault( "x", "0" );
426 CheckDefault( "y", "0" );
427 CheckDefault( "width", "0" );
428 CheckDefault( "height", "0" );
429 CheckDefault( "lefttop", "lefttop" );
430 CheckDefault( "rightbottom", "lefttop" );
431 CheckDefault( "autoresize", "false" );
432 CheckDefault( "help", "" );
434 const BuilderData::Video videoData( uniqueId( attr["id"] ),
435 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
436 atoi( attr["width"] ), atoi( attr["height" ]),
437 attr["lefttop"], attr["rightbottom"],
438 attr["visible"], convertBoolean( attr["autoresize"] ),
439 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
441 m_data.m_listVideo.push_back( videoData );
444 else if( rName == "Window" )
446 CheckDefault( "id", "none" );
447 CheckDefault( "visible", "true" );
448 CheckDefault( "x", "0" );
449 CheckDefault( "y", "0" );
450 CheckDefault( "dragdrop", "true" );
451 CheckDefault( "playondrop", "true" );
453 m_curWindowId = uniqueId( attr["id"] );
454 const BuilderData::Window window( m_curWindowId,
455 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
456 convertBoolean( attr["visible"] ),
457 convertBoolean( attr["dragdrop"] ),
458 convertBoolean( attr["playondrop"] ) );
459 m_data.m_listWindow.push_back( window );
464 void SkinParser::handleEndElement( const string &rName )
466 if( rName == "Group" )
468 m_xOffset -= m_xOffsetList.back();
469 m_yOffset -= m_yOffsetList.back();
470 m_xOffsetList.pop_back();
471 m_yOffsetList.pop_back();
473 else if( rName == "Playlist" )
477 else if( rName == "Playtree" )
484 bool SkinParser::convertBoolean( const char *value ) const
486 return strcmp( value, "true" ) == 0;
490 int SkinParser::convertColor( const char *transcolor ) const
492 unsigned long iRed, iGreen, iBlue;
493 iRed = iGreen = iBlue = 0;
494 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
495 return ( iRed << 16 | iGreen << 8 | iBlue );
499 string SkinParser::convertFileName( const char *fileName ) const
501 OSFactory *pFactory = OSFactory::instance( getIntf() );
502 return m_path + pFactory->getDirSeparator() + string( fileName );
506 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
507 const string &rAttribute ) const
509 int intValue = atoi( value );
511 if( intValue < minValue )
513 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
514 "expected range [%i, %i], using %i instead",
515 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
518 else if( intValue > maxValue )
520 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
521 "expected range [%i, %i], using %i instead",
522 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
532 const string SkinParser::generateId() const
537 snprintf( genId, 4, "%i", i++ );
539 string base = "_ReservedId_" + (string)genId;
545 const string SkinParser::uniqueId( const string &id )
549 if( m_idSet.find( id ) != m_idSet.end() )
551 // The id was already used
554 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
556 newId = generateId();
560 // OK, this is a new id
564 // Add the id to the set
565 m_idSet.insert( newId );