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( 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( "background", "none" );
343 CheckDefault( "nbimages", "1" );
344 CheckDefault( "tooltiptext", "" );
345 CheckDefault( "help", "" );
347 string newValue = attr["value"];
348 if( m_curListId != "" )
350 // Slider associated to a list
351 newValue = "playlist.slider";
353 else if( m_curTreeId != "" )
355 // Slider associated to a tree
356 newValue = "playtree.slider";
358 const BuilderData::Slider slider( uniqueId( attr["id"] ),
360 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
361 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
362 attr["over"], attr["points"], atoi( attr["thickness"] ),
363 newValue, attr["background"], atoi( attr["nbimages"] ),
364 attr["tooltiptext"], attr["help"], m_curLayer,
365 m_curWindowId, m_curLayoutId );
367 m_data.m_listSlider.push_back( slider );
370 else if( rName == "Text" )
372 RequireDefault( "font" );
373 CheckDefault( "id", "none" );
374 CheckDefault( "visible", "true" );
375 CheckDefault( "x", "0" );
376 CheckDefault( "y", "0" );
377 CheckDefault( "text", "" );
378 CheckDefault( "color", "#000000" );
379 CheckDefault( "width", "0" );
380 CheckDefault( "lefttop", "lefttop" );
381 CheckDefault( "rightbottom", "lefttop" );
382 CheckDefault( "help", "" );
384 const BuilderData::Text textData( uniqueId( attr["id"] ),
385 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
386 attr["visible"], attr["font"],
387 attr["text"], atoi( attr["width"] ),
388 attr["lefttop"], attr["rightbottom"],
389 convertColor( attr["color"] ), attr["help"], m_curLayer,
390 m_curWindowId, m_curLayoutId );
392 m_data.m_listText.push_back( textData );
395 else if( rName == "Theme" )
397 RequireDefault( "version" );
398 CheckDefault( "tooltipfont", "defaultfont" );
399 CheckDefault( "magnet", "15" );
400 CheckDefault( "alpha", "255" );
401 CheckDefault( "movealpha", "255" );
404 if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
406 msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
407 attr["version"], SKINS_DTD_VERSION );
411 const BuilderData::Theme theme( attr["tooltipfont"],
412 atoi( attr["magnet"] ),
413 convertInRange( attr["alpha"], 1, 255, "alpha" ),
414 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
415 m_data.m_listTheme.push_back( theme );
418 else if( rName == "ThemeInfo" )
420 msg_Info( getIntf(), "skin: %s author: %s", attr["name"],
424 else if( rName == "Video" )
426 CheckDefault( "id", "none" );
427 CheckDefault( "visible", "true" );
428 CheckDefault( "x", "0" );
429 CheckDefault( "y", "0" );
430 CheckDefault( "width", "0" );
431 CheckDefault( "height", "0" );
432 CheckDefault( "lefttop", "lefttop" );
433 CheckDefault( "rightbottom", "lefttop" );
434 CheckDefault( "autoresize", "false" );
435 CheckDefault( "help", "" );
437 const BuilderData::Video videoData( uniqueId( attr["id"] ),
438 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
439 atoi( attr["width"] ), atoi( attr["height" ]),
440 attr["lefttop"], attr["rightbottom"],
441 attr["visible"], convertBoolean( attr["autoresize"] ),
442 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
444 m_data.m_listVideo.push_back( videoData );
447 else if( rName == "Window" )
449 CheckDefault( "id", "none" );
450 CheckDefault( "visible", "true" );
451 CheckDefault( "x", "0" );
452 CheckDefault( "y", "0" );
453 CheckDefault( "dragdrop", "true" );
454 CheckDefault( "playondrop", "true" );
456 m_curWindowId = uniqueId( attr["id"] );
457 const BuilderData::Window window( m_curWindowId,
458 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
459 convertBoolean( attr["visible"] ),
460 convertBoolean( attr["dragdrop"] ),
461 convertBoolean( attr["playondrop"] ) );
462 m_data.m_listWindow.push_back( window );
467 void SkinParser::handleEndElement( const string &rName )
469 if( rName == "Group" )
471 m_xOffset -= m_xOffsetList.back();
472 m_yOffset -= m_yOffsetList.back();
473 m_xOffsetList.pop_back();
474 m_yOffsetList.pop_back();
476 else if( rName == "Playlist" )
480 else if( rName == "Playtree" )
487 bool SkinParser::convertBoolean( const char *value ) const
489 return strcmp( value, "true" ) == 0;
493 int SkinParser::convertColor( const char *transcolor ) const
495 unsigned long iRed, iGreen, iBlue;
496 iRed = iGreen = iBlue = 0;
497 sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
498 return ( iRed << 16 | iGreen << 8 | iBlue );
502 string SkinParser::convertFileName( const char *fileName ) const
504 OSFactory *pFactory = OSFactory::instance( getIntf() );
505 return m_path + pFactory->getDirSeparator() + string( fileName );
509 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
510 const string &rAttribute ) const
512 int intValue = atoi( value );
514 if( intValue < minValue )
516 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
517 "expected range [%i, %i], using %i instead",
518 rAttribute.c_str(), intValue, minValue, maxValue, minValue );
521 else if( intValue > maxValue )
523 msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
524 "expected range [%i, %i], using %i instead",
525 rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
535 const string SkinParser::generateId() const
540 snprintf( genId, 4, "%i", i++ );
542 string base = "_ReservedId_" + (string)genId;
548 const string SkinParser::uniqueId( const string &id )
552 if( m_idSet.find( id ) != m_idSet.end() )
554 // The id was already used
557 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
559 newId = generateId();
563 // OK, this is a new id
567 // Add the id to the set
568 m_idSet.insert( newId );