]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
8cae5c512f6cdb27cd15cc81786e942f67de57f7
[vlc] / modules / gui / skins2 / parser / skin_parser.cpp
1 /*****************************************************************************
2  * skin_parser.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "skin_parser.hpp"
25 #include "../src/os_factory.hpp"
26 #include "interpreter.hpp"
27 #include <math.h>
28
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 )
33 {
34     // Make sure the data is allocated
35     if( m_pData == NULL )
36     {
37         m_pData = new BuilderData();
38     }
39
40     // Special id, we don't want any control to have the same one
41     m_idSet.insert( "none" );
42     // At the beginning, there is no Panel
43     m_panelStack.push_back( "none" );
44 }
45
46
47 SkinParser::~SkinParser()
48 {
49     if( m_ownData )
50     {
51         delete m_pData;
52     }
53 }
54
55 inline bool SkinParser::MissingAttr( AttrList_t &attr, const string &name,
56                                      const char *a )
57 {
58     if( attr.find(a) == attr.end() )
59     {
60         msg_Err( getIntf(), "bad theme (element: %s, missing attribute: %s)",
61                  name.c_str(), a );
62         m_errors = true; return true;
63     }
64     return false;
65 }
66
67 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
68 {
69 #define RequireAttr( attr, name, a ) \
70     if( MissingAttr( attr, name, a ) ) return;
71
72     if( rName == "Include" )
73     {
74         RequireAttr( attr, rName, "file" );
75
76         OSFactory *pFactory = OSFactory::instance( getIntf() );
77         string fullPath = m_path + pFactory->getDirSeparator() + attr["file"];
78         msg_Dbg( getIntf(), "opening included XML file: %s", fullPath.c_str() );
79         // FIXME: We do not use the DTD to validate the included XML file,
80         // as the parser seems to dislike it otherwise...
81         SkinParser subParser( getIntf(), fullPath.c_str(), false, m_pData );
82         subParser.parse();
83     }
84
85     else if( rName == "IniFile" )
86     {
87         RequireAttr( attr, rName, "id" );
88         RequireAttr( attr, rName, "file" );
89
90         const BuilderData::IniFile iniFile( uniqueId( attr["id"] ),
91                 attr["file"] );
92         m_pData->m_listIniFile.push_back( iniFile );
93     }
94
95     else if( rName == "Anchor" )
96     {
97         RequireAttr( attr, rName, "priority" );
98         DefaultAttr( attr, "x", "0" );
99         DefaultAttr( attr, "y", "0" );
100         DefaultAttr( attr, "lefttop", "lefttop" );
101         DefaultAttr( attr, "points", "(0,0)" );
102         DefaultAttr( attr, "range", "10" );
103
104         const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
105                 atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
106                 atoi( attr["range"] ), atoi( attr["priority"] ),
107                 attr["points"], m_curLayoutId );
108         m_pData->m_listAnchor.push_back( anchor );
109     }
110
111     else if( rName == "Bitmap" )
112     {
113         RequireAttr( attr, rName, "id" );
114         RequireAttr( attr, rName, "file" );
115         RequireAttr( attr, rName, "alphacolor" );
116         DefaultAttr( attr, "nbframes", "1" );
117         DefaultAttr( attr, "fps", "4" );
118         DefaultAttr( attr, "loop", "0" );
119
120         m_curBitmapId = uniqueId( attr["id"] );
121         const BuilderData::Bitmap bitmap( m_curBitmapId,
122                 attr["file"], convertColor( attr["alphacolor"] ),
123                 atoi( attr["nbframes"] ), atoi( attr["fps"] ),
124                 atoi( attr["loop"] ) );
125         m_pData->m_listBitmap.push_back( bitmap );
126     }
127
128     else if( rName == "SubBitmap" )
129     {
130         RequireAttr( attr, rName, "id" );
131         RequireAttr( attr, rName, "x" );
132         RequireAttr( attr, rName, "y" );
133         RequireAttr( attr, rName, "width" );
134         RequireAttr( attr, rName, "height" );
135         DefaultAttr( attr, "nbframes", "1" );
136         DefaultAttr( attr, "fps", "4" );
137         DefaultAttr( attr, "loop", "0" );
138
139         const BuilderData::SubBitmap bitmap( uniqueId( attr["id"] ),
140                 m_curBitmapId, atoi( attr["x"] ), atoi( attr["y"] ),
141                 atoi( attr["width"] ), atoi( attr["height"] ),
142                 atoi( attr["nbframes"] ), atoi( attr["fps"] ),
143                 atoi( attr["loop"] ) );
144         m_pData->m_listSubBitmap.push_back( bitmap );
145     }
146
147     else if( rName == "BitmapFont" )
148     {
149         RequireAttr( attr, rName, "id" );
150         RequireAttr( attr, rName, "file" );
151         DefaultAttr( attr, "type", "digits" );
152
153         const BuilderData::BitmapFont font( uniqueId( attr["id"] ),
154                 attr["file"], attr["type"] );
155         m_pData->m_listBitmapFont.push_back( font );
156     }
157
158     else if( rName == "PopupMenu" )
159     {
160         RequireAttr( attr, rName, "id" );
161
162         m_popupPosList.push_back(0);
163         m_curPopupId = uniqueId( attr["id"] );
164         const BuilderData::PopupMenu popup( m_curPopupId );
165         m_pData->m_listPopupMenu.push_back( popup );
166     }
167
168     else if( rName == "MenuItem" )
169     {
170         RequireAttr( attr, rName, "label" );
171         DefaultAttr( attr, "action", "none" );
172
173         const BuilderData::MenuItem item( attr["label"], attr["action"],
174                                           m_popupPosList.back(),
175                                           m_curPopupId );
176         m_pData->m_listMenuItem.push_back( item );
177         m_popupPosList.back()++;
178     }
179
180     else if( rName == "MenuSeparator" )
181     {
182         const BuilderData::MenuSeparator sep( m_popupPosList.back(),
183                                               m_curPopupId );
184         m_pData->m_listMenuSeparator.push_back( sep );
185         m_popupPosList.back()++;
186     }
187
188     else if( rName == "Button" )
189     {
190         RequireAttr( attr, rName, "up" );
191         DefaultAttr( attr, "id", "none" );
192         DefaultAttr( attr, "visible", "true" );
193         DefaultAttr( attr, "x", "0" );
194         DefaultAttr( attr, "y", "0" );
195         DefaultAttr( attr, "lefttop", "lefttop" );
196         DefaultAttr( attr, "rightbottom", "lefttop" );
197         DefaultAttr( attr, "xkeepratio", "false" );
198         DefaultAttr( attr, "ykeepratio", "false" );
199         DefaultAttr( attr, "down", "none" );
200         DefaultAttr( attr, "over", "none" );
201         DefaultAttr( attr, "action", "none" );
202         DefaultAttr( attr, "tooltiptext", "" );
203         DefaultAttr( attr, "help", "" );
204
205         const BuilderData::Button button( uniqueId( attr["id"] ),
206                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
207                 attr["lefttop"], attr["rightbottom"],
208                 convertBoolean( attr["xkeepratio"] ),
209                 convertBoolean( attr["ykeepratio"] ), attr["visible"],
210                 attr["up"], attr["down"], attr["over"], attr["action"],
211                 attr["tooltiptext"], attr["help"],
212                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
213         m_curLayer++;
214         m_pData->m_listButton.push_back( button );
215     }
216
217     else if( rName == "Checkbox" )
218     {
219         RequireAttr( attr, rName, "up1" );
220         RequireAttr( attr, rName, "up2" );
221         RequireAttr( attr, rName, "state" );
222         DefaultAttr( attr, "id", "none" );
223         DefaultAttr( attr, "visible", "true" );
224         DefaultAttr( attr, "x", "0" );
225         DefaultAttr( attr, "y", "0" );
226         DefaultAttr( attr, "lefttop", "lefttop" );
227         DefaultAttr( attr, "rightbottom", "lefttop" );
228         DefaultAttr( attr, "xkeepratio", "false" );
229         DefaultAttr( attr, "ykeepratio", "false" );
230         DefaultAttr( attr, "down1", "none" );
231         DefaultAttr( attr, "over1", "none" );
232         DefaultAttr( attr, "down2", "none" );
233         DefaultAttr( attr, "over2", "none" );
234         DefaultAttr( attr, "action1", "none" );
235         DefaultAttr( attr, "action2", "none" );
236         DefaultAttr( attr, "tooltiptext1", "" );
237         DefaultAttr( attr, "tooltiptext2", "" );
238         DefaultAttr( attr, "help", "" );
239
240         const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
241                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
242                 attr["lefttop"], attr["rightbottom"],
243                 convertBoolean( attr["xkeepratio"] ),
244                 convertBoolean( attr["ykeepratio"] ), attr["visible"],
245                 attr["up1"], attr["down1"], attr["over1"],
246                 attr["up2"], attr["down2"], attr["over2"], attr["state"],
247                 attr["action1"], attr["action2"], attr["tooltiptext1"],
248                 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
249                 m_curLayoutId, m_panelStack.back() );
250         m_curLayer++;
251         m_pData->m_listCheckbox.push_back( checkbox );
252     }
253
254     else if( rName == "Font" )
255     {
256         RequireAttr( attr, rName, "id" );
257         RequireAttr( attr, rName, "file" );
258         DefaultAttr( attr, "size", "12" );
259
260         const BuilderData::Font fontData( uniqueId( attr["id"] ),
261                 attr["file"], atoi( attr["size"] ) );
262         m_pData->m_listFont.push_back( fontData );
263     }
264
265     else if( rName == "Group" )
266     {
267         DefaultAttr( attr, "x", "0" );
268         DefaultAttr( attr, "y", "0" );
269
270         m_xOffset += atoi( attr["x"] );
271         m_yOffset += atoi( attr["y"] );
272         m_xOffsetList.push_back( atoi( attr["x"] ) );
273         m_yOffsetList.push_back( atoi( attr["y"] ) );
274     }
275
276     else if( rName == "Image" )
277     {
278         RequireAttr( attr, rName, "image" );
279         DefaultAttr( attr, "id", "none" );
280         DefaultAttr( attr, "visible", "true" );
281         DefaultAttr( attr, "x", "0" );
282         DefaultAttr( attr, "y", "0" );
283         DefaultAttr( attr, "lefttop", "lefttop" );
284         DefaultAttr( attr, "rightbottom", "lefttop" );
285         DefaultAttr( attr, "xkeepratio", "false" );
286         DefaultAttr( attr, "ykeepratio", "false" );
287         DefaultAttr( attr, "action", "none" );
288         DefaultAttr( attr, "action2", "none" );
289         DefaultAttr( attr, "resize", "mosaic" );
290         DefaultAttr( attr, "help", "" );
291
292         const BuilderData::Image imageData( uniqueId( attr["id"] ),
293                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
294                 attr["lefttop"], attr["rightbottom"],
295                 convertBoolean( attr["xkeepratio"] ),
296                 convertBoolean( attr["ykeepratio"] ), attr["visible"],
297                 attr["image"], attr["action"], attr["action2"], attr["resize"],
298                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
299                 m_panelStack.back() );
300         m_curLayer++;
301         m_pData->m_listImage.push_back( imageData );
302     }
303
304     else if( rName == "Layout" )
305     {
306         RequireAttr( attr, rName, "width" );
307         RequireAttr( attr, rName, "height" );
308         DefaultAttr( attr, "id", "none" );
309         DefaultAttr( attr, "minwidth", "-1" );
310         DefaultAttr( attr, "maxwidth", "-1" );
311         DefaultAttr( attr, "minheight", "-1" );
312         DefaultAttr( attr, "maxheight", "-1" );
313
314         m_curLayoutId = uniqueId( attr["id"] );
315         const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
316                 atoi( attr["height"] ), atoi( attr["minwidth"] ),
317                 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
318                 atoi( attr["maxheight"] ), m_curWindowId );
319         m_pData->m_listLayout.push_back( layout );
320         m_curLayer = 0;
321     }
322
323     else if( rName == "Panel" )
324     {
325         DefaultAttr( attr, "x", "0" );
326         DefaultAttr( attr, "y", "0" );
327         DefaultAttr( attr, "lefttop", "lefttop" );
328         DefaultAttr( attr, "rightbottom", "lefttop" );
329         DefaultAttr( attr, "xkeepratio", "false" );
330         DefaultAttr( attr, "ykeepratio", "false" );
331         RequireAttr( attr, rName, "width" );
332         RequireAttr( attr, rName, "height" );
333
334         string panelId = uniqueId( "none" );
335         const BuilderData::Panel panel( panelId,
336                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
337                 attr["lefttop"], attr["rightbottom"],
338                 convertBoolean( attr["xkeepratio"] ),
339                 convertBoolean( attr["ykeepratio"] ),
340                 atoi( attr["width"] ), atoi( attr["height" ] ),
341                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
342         m_curLayer++;
343         m_pData->m_listPanel.push_back( panel );
344         // Add the panel to the stack
345         m_panelStack.push_back( panelId );
346     }
347
348     else if( rName == "Playlist" )
349     {
350         RequireAttr( attr, rName, "id" );
351         RequireAttr( attr, rName, "font" );
352         DefaultAttr( attr, "visible", "true" );
353         DefaultAttr( attr, "flat", "true" ); // Only difference here
354         DefaultAttr( attr, "x", "0" );
355         DefaultAttr( attr, "y", "0" );
356         DefaultAttr( attr, "width", "0" );
357         DefaultAttr( attr, "height", "0" );
358         DefaultAttr( attr, "lefttop", "lefttop" );
359         DefaultAttr( attr, "rightbottom", "lefttop" );
360         DefaultAttr( attr, "xkeepratio", "false" );
361         DefaultAttr( attr, "ykeepratio", "false" );
362         DefaultAttr( attr, "bgimage", "none" );
363         DefaultAttr( attr, "itemimage", "none" );
364         DefaultAttr( attr, "openimage", "none" );
365         DefaultAttr( attr, "closedimage", "none" );
366         DefaultAttr( attr, "fgcolor", "#000000" );
367         DefaultAttr( attr, "playcolor", "#FF0000" );
368         DefaultAttr( attr, "bgcolor1", "#FFFFFF" );
369         DefaultAttr( attr, "bgcolor2", "#FFFFFF" );
370         DefaultAttr( attr, "selcolor", "#0000FF" );
371         DefaultAttr( attr, "help", "" );
372
373         m_curTreeId = uniqueId( attr["id"] );
374         const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
375                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
376                 attr["flat"],
377                 atoi( attr["width"]), atoi( attr["height"] ),
378                 attr["lefttop"], attr["rightbottom"],
379                 convertBoolean( attr["xkeepratio"] ),
380                 convertBoolean( attr["ykeepratio"] ),
381                 attr["font"], "playtree",
382                 attr["bgimage"], attr["itemimage"],
383                 attr["openimage"], attr["closedimage"],
384                 attr["fgcolor"],
385                 attr["playcolor"],
386                 attr["bgcolor1"],
387                 attr["bgcolor2"],
388                 attr["selcolor"], attr["help"],
389                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
390         m_curLayer++;
391         m_pData->m_listTree.push_back( treeData );
392     }
393     else if( rName == "Playtree" )
394     {
395         RequireAttr( attr, rName, "id" );
396         RequireAttr( attr, rName, "font" );
397         DefaultAttr( attr, "visible", "true" );
398         DefaultAttr( attr, "flat", "false" );
399         DefaultAttr( attr, "x", "0" );
400         DefaultAttr( attr, "y", "0" );
401         DefaultAttr( attr, "width", "0" );
402         DefaultAttr( attr, "height", "0" );
403         DefaultAttr( attr, "lefttop", "lefttop" );
404         DefaultAttr( attr, "rightbottom", "lefttop" );
405         DefaultAttr( attr, "xkeepratio", "false" );
406         DefaultAttr( attr, "ykeepratio", "false" );
407         DefaultAttr( attr, "bgimage", "none" );
408         DefaultAttr( attr, "itemimage", "none" );
409         DefaultAttr( attr, "openimage", "none" );
410         DefaultAttr( attr, "closedimage", "none" );
411         DefaultAttr( attr, "fgcolor", "#000000" );
412         DefaultAttr( attr, "playcolor", "#FF0000" );
413         DefaultAttr( attr, "bgcolor1", "#FFFFFF" );
414         DefaultAttr( attr, "bgcolor2", "#FFFFFF" );
415         DefaultAttr( attr, "selcolor", "#0000FF" );
416         DefaultAttr( attr, "help", "" );
417
418         m_curTreeId = uniqueId( attr["id"] );
419         const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
420                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
421                 attr["flat"],
422                 atoi( attr["width"]), atoi( attr["height"] ),
423                 attr["lefttop"], attr["rightbottom"],
424                 convertBoolean( attr["xkeepratio"] ),
425                 convertBoolean( attr["ykeepratio"] ),
426                 attr["font"], "playtree",
427                 attr["bgimage"], attr["itemimage"],
428                 attr["openimage"], attr["closedimage"],
429                 attr["fgcolor"], attr["playcolor"],
430                 attr["bgcolor1"], attr["bgcolor2"],
431                 attr["selcolor"], attr["help"],
432                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
433         m_curLayer++;
434         m_pData->m_listTree.push_back( treeData );
435     }
436
437     else if( rName == "RadialSlider" )
438     {
439         RequireAttr( attr, rName, "sequence" );
440         RequireAttr( attr, rName, "nbimages" );
441         DefaultAttr( attr, "id", "none" );
442         DefaultAttr( attr, "visible", "true" );
443         DefaultAttr( attr, "x", "0" );
444         DefaultAttr( attr, "y", "0" );
445         DefaultAttr( attr, "lefttop", "lefttop" );
446         DefaultAttr( attr, "rightbottom", "lefttop" );
447         DefaultAttr( attr, "xkeepratio", "false" );
448         DefaultAttr( attr, "ykeepratio", "false" );
449         DefaultAttr( attr, "minangle", "0" );
450         DefaultAttr( attr, "maxangle", "360" );
451         DefaultAttr( attr, "value", "none" );
452         DefaultAttr( attr, "tooltiptext", "" );
453         DefaultAttr( attr, "help", "" );
454
455         const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
456                 attr["visible"],
457                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
458                 attr["lefttop"], attr["rightbottom"],
459                 convertBoolean( attr["xkeepratio"] ),
460                 convertBoolean( attr["ykeepratio"] ), attr["sequence"],
461                 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
462                 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
463                 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
464                 m_curLayoutId, m_panelStack.back() );
465         m_curLayer++;
466         m_pData->m_listRadialSlider.push_back( radial );
467     }
468
469     else if( rName == "Slider" )
470     {
471         RequireAttr( attr, rName, "up" );
472         RequireAttr( attr, rName, "points" );
473         DefaultAttr( attr, "id", "none" );
474         DefaultAttr( attr, "visible", "true" );
475         DefaultAttr( attr, "x", "0" );
476         DefaultAttr( attr, "y", "0" );
477         DefaultAttr( attr, "lefttop", "lefttop" );
478         DefaultAttr( attr, "rightbottom", "lefttop" );
479         DefaultAttr( attr, "xkeepratio", "false" );
480         DefaultAttr( attr, "ykeepratio", "false" );
481         DefaultAttr( attr, "down", "none" );
482         DefaultAttr( attr, "over", "none" );
483         DefaultAttr( attr, "thickness", "10" );
484         DefaultAttr( attr, "value", "none" );
485         DefaultAttr( attr, "tooltiptext", "" );
486         DefaultAttr( attr, "help", "" );
487
488         string newValue = attr["value"];
489         if( m_curTreeId != "" )
490         {
491             // Slider associated to a tree
492             newValue = "playtree.slider";
493         }
494         const BuilderData::Slider slider( uniqueId( attr["id"] ),
495                 attr["visible"], atoi( attr["x"] ) + m_xOffset,
496                 atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
497                 attr["rightbottom"], convertBoolean( attr["xkeepratio"] ),
498                 convertBoolean( attr["ykeepratio"] ), attr["up"], attr["down"],
499                 attr["over"], attr["points"], atoi( attr["thickness"] ),
500                 newValue, "none", 0, 0, 0, 0, attr["tooltiptext"],
501                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
502                 m_panelStack.back() );
503         m_curLayer++;
504         m_pData->m_listSlider.push_back( slider );
505     }
506
507     else if( rName == "SliderBackground" )
508     {
509         RequireAttr( attr, rName, "image" );
510         DefaultAttr( attr, "nbhoriz", "1" );
511         DefaultAttr( attr, "nbvert", "1" );
512         DefaultAttr( attr, "padhoriz", "0" );
513         DefaultAttr( attr, "padvert", "0" );
514
515         // Retrieve the current slider data
516         BuilderData::Slider &slider = m_pData->m_listSlider.back();
517
518         slider.m_imageId = attr["image"];
519         slider.m_nbHoriz = atoi( attr["nbhoriz"] );
520         slider.m_nbVert = atoi( attr["nbvert"] );
521         slider.m_padHoriz = atoi( attr["padhoriz"] );
522         slider.m_padVert = atoi( attr["padvert"] );
523     }
524
525     else if( rName == "Text" )
526     {
527         RequireAttr( attr, rName, "font" );
528         DefaultAttr( attr, "id", "none" );
529         DefaultAttr( attr, "visible", "true" );
530         DefaultAttr( attr, "x", "0" );
531         DefaultAttr( attr, "y", "0" );
532         DefaultAttr( attr, "text", "" );
533         DefaultAttr( attr, "color", "#000000" );
534         DefaultAttr( attr, "scrolling", "auto" );
535         DefaultAttr( attr, "alignment", "left" );
536         DefaultAttr( attr, "width", "0" );
537         DefaultAttr( attr, "lefttop", "lefttop" );
538         DefaultAttr( attr, "rightbottom", "lefttop" );
539         DefaultAttr( attr, "xkeepratio", "false" );
540         DefaultAttr( attr, "ykeepratio", "false" );
541         DefaultAttr( attr, "help", "" );
542
543         const BuilderData::Text textData( uniqueId( attr["id"] ),
544                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
545                 attr["visible"], attr["font"],
546                 attr["text"], atoi( attr["width"] ),
547                 attr["lefttop"], attr["rightbottom"],
548                 convertBoolean( attr["xkeepratio"] ),
549                 convertBoolean( attr["ykeepratio"] ),
550                 convertColor( attr["color"] ),
551                 attr["scrolling"], attr["alignment"],
552                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
553                 m_panelStack.back() );
554         m_curLayer++;
555         m_pData->m_listText.push_back( textData );
556     }
557
558     else if( rName == "Theme" )
559     {
560         RequireAttr( attr, rName, "version" );
561         DefaultAttr( attr, "tooltipfont", "defaultfont" );
562         DefaultAttr( attr, "magnet", "15" );
563         DefaultAttr( attr, "alpha", "255" );
564         DefaultAttr( attr, "movealpha", "255" );
565
566         // Check the version
567         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
568         {
569             msg_Err( getIntf(), "bad theme version : %s (you need version %s)",
570                      attr["version"], SKINS_DTD_VERSION );
571             m_errors = true;
572             return;
573         }
574         const BuilderData::Theme theme( attr["tooltipfont"],
575                 atoi( attr["magnet"] ),
576                 convertInRange( attr["alpha"], 1, 255, "alpha" ),
577                 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
578         m_pData->m_listTheme.push_back( theme );
579     }
580
581     else if( rName == "ThemeInfo" )
582     {
583         DefaultAttr( attr, "name", "" );
584         DefaultAttr( attr, "author", "" );
585         DefaultAttr( attr, "email", "" );
586         DefaultAttr( attr, "website", "" );
587         msg_Info( getIntf(), "skin: %s  author: %s", attr["name"],
588                   attr["author"] );
589     }
590
591     else if( rName == "Video" )
592     {
593         DefaultAttr( attr, "id", "none" );
594         DefaultAttr( attr, "visible", "true" );
595         DefaultAttr( attr, "x", "0" );
596         DefaultAttr( attr, "y", "0" );
597         DefaultAttr( attr, "width", "0" );
598         DefaultAttr( attr, "height", "0" );
599         DefaultAttr( attr, "lefttop", "lefttop" );
600         DefaultAttr( attr, "rightbottom", "lefttop" );
601         DefaultAttr( attr, "xkeepratio", "false" );
602         DefaultAttr( attr, "ykeepratio", "false" );
603         DefaultAttr( attr, "autoresize", "false" );
604         DefaultAttr( attr, "help", "" );
605
606         const BuilderData::Video videoData( uniqueId( attr["id"] ),
607                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
608                 atoi( attr["width"] ), atoi( attr["height" ]),
609                 attr["lefttop"], attr["rightbottom"],
610                 convertBoolean( attr["xkeepratio"] ),
611                 convertBoolean( attr["ykeepratio"] ),
612                 attr["visible"], convertBoolean( attr["autoresize"] ),
613                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
614                 m_panelStack.back() );
615         m_curLayer++;
616         m_pData->m_listVideo.push_back( videoData );
617     }
618
619     else if( rName == "Window" )
620     {
621         DefaultAttr( attr, "id", "none" );
622         DefaultAttr( attr, "visible", "true" );
623         DefaultAttr( attr, "x", "0" );
624         DefaultAttr( attr, "y", "0" );
625         DefaultAttr( attr, "dragdrop", "true" );
626         DefaultAttr( attr, "playondrop", "true" );
627
628         m_curWindowId = uniqueId( attr["id"] );
629         const BuilderData::Window window( m_curWindowId,
630                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
631                 convertBoolean( attr["visible"] ),
632                 convertBoolean( attr["dragdrop"] ),
633                 convertBoolean( attr["playondrop"] ) );
634         m_pData->m_listWindow.push_back( window );
635     }
636 #undef  RequireAttr
637 }
638
639
640 void SkinParser::handleEndElement( const string &rName )
641 {
642     if( rName == "Group" )
643     {
644         m_xOffset -= m_xOffsetList.back();
645         m_yOffset -= m_yOffsetList.back();
646         m_xOffsetList.pop_back();
647         m_yOffsetList.pop_back();
648     }
649     else if( rName == "Playtree" || rName == "Playlist" )
650     {
651         m_curTreeId = "";
652     }
653     else if( rName == "Popup" )
654     {
655         m_curPopupId = "";
656         m_popupPosList.pop_back();
657     }
658     else if( rName == "Panel" )
659     {
660         m_panelStack.pop_back();
661     }
662 }
663
664
665 bool SkinParser::convertBoolean( const char *value ) const
666 {
667     return strcmp( value, "true" ) == 0;
668 }
669
670
671 int SkinParser::convertColor( const char *transcolor )
672 {
673     // TODO: move to the builder
674     unsigned long iRed, iGreen, iBlue;
675     iRed = iGreen = iBlue = 0;
676     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
677     return ( iRed << 16 | iGreen << 8 | iBlue );
678 }
679
680
681 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
682                                 const string &rAttribute ) const
683 {
684     int intValue = atoi( value );
685
686     if( intValue < minValue )
687     {
688         msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
689                   "expected range [%i, %i], using %i instead",
690                   rAttribute.c_str(), intValue, minValue, maxValue, minValue );
691         return minValue;
692     }
693     else if( intValue > maxValue )
694     {
695         msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
696                   "expected range [%i, %i], using %i instead",
697                   rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
698         return maxValue;
699     }
700     else
701     {
702         return intValue;
703     }
704 }
705
706
707 const string SkinParser::generateId() const
708 {
709     static int i = 1;
710
711     char genId[5];
712     snprintf( genId, 4, "%i", i++ );
713
714     string base = "_ReservedId_" + (string)genId;
715
716     return base;
717 }
718
719
720 const string SkinParser::uniqueId( const string &id )
721 {
722     string newId;
723
724     if( m_idSet.find( id ) != m_idSet.end() )
725     {
726         // The id was already used
727         if( id != "none" )
728         {
729             msg_Warn( getIntf(), "non-unique id: %s", id.c_str() );
730         }
731         newId = generateId();
732     }
733     else
734     {
735         // OK, this is a new id
736         newId = id;
737     }
738
739     // Add the id to the set
740     m_idSet.insert( newId );
741
742     return newId;
743 }