]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
skins2: implement art display in image controls
[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         DefaultAttr( attr, "art", "false" );
292
293         const BuilderData::Image imageData( uniqueId( attr["id"] ),
294                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
295                 attr["lefttop"], attr["rightbottom"],
296                 convertBoolean( attr["xkeepratio"] ),
297                 convertBoolean( attr["ykeepratio"] ), attr["visible"],
298                 attr["image"], attr["action"], attr["action2"], attr["resize"],
299                 attr["help"], convertBoolean( attr["art"] ),
300                 m_curLayer, m_curWindowId, m_curLayoutId,
301                 m_panelStack.back() );
302         m_curLayer++;
303         m_pData->m_listImage.push_back( imageData );
304     }
305
306     else if( rName == "Layout" )
307     {
308         RequireAttr( attr, rName, "width" );
309         RequireAttr( attr, rName, "height" );
310         DefaultAttr( attr, "id", "none" );
311         DefaultAttr( attr, "minwidth", "-1" );
312         DefaultAttr( attr, "maxwidth", "-1" );
313         DefaultAttr( attr, "minheight", "-1" );
314         DefaultAttr( attr, "maxheight", "-1" );
315
316         m_curLayoutId = uniqueId( attr["id"] );
317         const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
318                 atoi( attr["height"] ), atoi( attr["minwidth"] ),
319                 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
320                 atoi( attr["maxheight"] ), m_curWindowId );
321         m_pData->m_listLayout.push_back( layout );
322         m_curLayer = 0;
323     }
324
325     else if( rName == "Panel" )
326     {
327         DefaultAttr( attr, "x", "0" );
328         DefaultAttr( attr, "y", "0" );
329         DefaultAttr( attr, "lefttop", "lefttop" );
330         DefaultAttr( attr, "rightbottom", "lefttop" );
331         DefaultAttr( attr, "xkeepratio", "false" );
332         DefaultAttr( attr, "ykeepratio", "false" );
333         RequireAttr( attr, rName, "width" );
334         RequireAttr( attr, rName, "height" );
335
336         string panelId = uniqueId( "none" );
337         const BuilderData::Panel panel( panelId,
338                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
339                 attr["lefttop"], attr["rightbottom"],
340                 convertBoolean( attr["xkeepratio"] ),
341                 convertBoolean( attr["ykeepratio"] ),
342                 atoi( attr["width"] ), atoi( attr["height" ] ),
343                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
344         m_curLayer++;
345         m_pData->m_listPanel.push_back( panel );
346         // Add the panel to the stack
347         m_panelStack.push_back( panelId );
348     }
349
350     else if( rName == "Playlist" )
351     {
352         RequireAttr( attr, rName, "id" );
353         RequireAttr( attr, rName, "font" );
354         DefaultAttr( attr, "visible", "true" );
355         DefaultAttr( attr, "flat", "true" ); // Only difference here
356         DefaultAttr( attr, "x", "0" );
357         DefaultAttr( attr, "y", "0" );
358         DefaultAttr( attr, "width", "0" );
359         DefaultAttr( attr, "height", "0" );
360         DefaultAttr( attr, "lefttop", "lefttop" );
361         DefaultAttr( attr, "rightbottom", "lefttop" );
362         DefaultAttr( attr, "xkeepratio", "false" );
363         DefaultAttr( attr, "ykeepratio", "false" );
364         DefaultAttr( attr, "bgimage", "none" );
365         DefaultAttr( attr, "itemimage", "none" );
366         DefaultAttr( attr, "openimage", "none" );
367         DefaultAttr( attr, "closedimage", "none" );
368         DefaultAttr( attr, "fgcolor", "#000000" );
369         DefaultAttr( attr, "playcolor", "#FF0000" );
370         DefaultAttr( attr, "bgcolor1", "#FFFFFF" );
371         DefaultAttr( attr, "bgcolor2", "#FFFFFF" );
372         DefaultAttr( attr, "selcolor", "#0000FF" );
373         DefaultAttr( attr, "help", "" );
374
375         m_curTreeId = uniqueId( attr["id"] );
376         const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
377                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
378                 attr["flat"],
379                 atoi( attr["width"]), atoi( attr["height"] ),
380                 attr["lefttop"], attr["rightbottom"],
381                 convertBoolean( attr["xkeepratio"] ),
382                 convertBoolean( attr["ykeepratio"] ),
383                 attr["font"], "playtree",
384                 attr["bgimage"], attr["itemimage"],
385                 attr["openimage"], attr["closedimage"],
386                 attr["fgcolor"],
387                 attr["playcolor"],
388                 attr["bgcolor1"],
389                 attr["bgcolor2"],
390                 attr["selcolor"], attr["help"],
391                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
392         m_curLayer++;
393         m_pData->m_listTree.push_back( treeData );
394     }
395     else if( rName == "Playtree" )
396     {
397         RequireAttr( attr, rName, "id" );
398         RequireAttr( attr, rName, "font" );
399         DefaultAttr( attr, "visible", "true" );
400         DefaultAttr( attr, "flat", "false" );
401         DefaultAttr( attr, "x", "0" );
402         DefaultAttr( attr, "y", "0" );
403         DefaultAttr( attr, "width", "0" );
404         DefaultAttr( attr, "height", "0" );
405         DefaultAttr( attr, "lefttop", "lefttop" );
406         DefaultAttr( attr, "rightbottom", "lefttop" );
407         DefaultAttr( attr, "xkeepratio", "false" );
408         DefaultAttr( attr, "ykeepratio", "false" );
409         DefaultAttr( attr, "bgimage", "none" );
410         DefaultAttr( attr, "itemimage", "none" );
411         DefaultAttr( attr, "openimage", "none" );
412         DefaultAttr( attr, "closedimage", "none" );
413         DefaultAttr( attr, "fgcolor", "#000000" );
414         DefaultAttr( attr, "playcolor", "#FF0000" );
415         DefaultAttr( attr, "bgcolor1", "#FFFFFF" );
416         DefaultAttr( attr, "bgcolor2", "#FFFFFF" );
417         DefaultAttr( attr, "selcolor", "#0000FF" );
418         DefaultAttr( attr, "help", "" );
419
420         m_curTreeId = uniqueId( attr["id"] );
421         const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
422                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
423                 attr["flat"],
424                 atoi( attr["width"]), atoi( attr["height"] ),
425                 attr["lefttop"], attr["rightbottom"],
426                 convertBoolean( attr["xkeepratio"] ),
427                 convertBoolean( attr["ykeepratio"] ),
428                 attr["font"], "playtree",
429                 attr["bgimage"], attr["itemimage"],
430                 attr["openimage"], attr["closedimage"],
431                 attr["fgcolor"], attr["playcolor"],
432                 attr["bgcolor1"], attr["bgcolor2"],
433                 attr["selcolor"], attr["help"],
434                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
435         m_curLayer++;
436         m_pData->m_listTree.push_back( treeData );
437     }
438
439     else if( rName == "RadialSlider" )
440     {
441         RequireAttr( attr, rName, "sequence" );
442         RequireAttr( attr, rName, "nbimages" );
443         DefaultAttr( attr, "id", "none" );
444         DefaultAttr( attr, "visible", "true" );
445         DefaultAttr( attr, "x", "0" );
446         DefaultAttr( attr, "y", "0" );
447         DefaultAttr( attr, "lefttop", "lefttop" );
448         DefaultAttr( attr, "rightbottom", "lefttop" );
449         DefaultAttr( attr, "xkeepratio", "false" );
450         DefaultAttr( attr, "ykeepratio", "false" );
451         DefaultAttr( attr, "minangle", "0" );
452         DefaultAttr( attr, "maxangle", "360" );
453         DefaultAttr( attr, "value", "none" );
454         DefaultAttr( attr, "tooltiptext", "" );
455         DefaultAttr( attr, "help", "" );
456
457         const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
458                 attr["visible"],
459                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
460                 attr["lefttop"], attr["rightbottom"],
461                 convertBoolean( attr["xkeepratio"] ),
462                 convertBoolean( attr["ykeepratio"] ), attr["sequence"],
463                 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
464                 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
465                 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
466                 m_curLayoutId, m_panelStack.back() );
467         m_curLayer++;
468         m_pData->m_listRadialSlider.push_back( radial );
469     }
470
471     else if( rName == "Slider" )
472     {
473         RequireAttr( attr, rName, "up" );
474         RequireAttr( attr, rName, "points" );
475         DefaultAttr( attr, "id", "none" );
476         DefaultAttr( attr, "visible", "true" );
477         DefaultAttr( attr, "x", "0" );
478         DefaultAttr( attr, "y", "0" );
479         DefaultAttr( attr, "lefttop", "lefttop" );
480         DefaultAttr( attr, "rightbottom", "lefttop" );
481         DefaultAttr( attr, "xkeepratio", "false" );
482         DefaultAttr( attr, "ykeepratio", "false" );
483         DefaultAttr( attr, "down", "none" );
484         DefaultAttr( attr, "over", "none" );
485         DefaultAttr( attr, "thickness", "10" );
486         DefaultAttr( attr, "value", "none" );
487         DefaultAttr( attr, "tooltiptext", "" );
488         DefaultAttr( attr, "help", "" );
489
490         string newValue = attr["value"];
491         if( m_curTreeId != "" )
492         {
493             // Slider associated to a tree
494             newValue = "playtree.slider";
495         }
496         const BuilderData::Slider slider( uniqueId( attr["id"] ),
497                 attr["visible"], atoi( attr["x"] ) + m_xOffset,
498                 atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
499                 attr["rightbottom"], convertBoolean( attr["xkeepratio"] ),
500                 convertBoolean( attr["ykeepratio"] ), attr["up"], attr["down"],
501                 attr["over"], attr["points"], atoi( attr["thickness"] ),
502                 newValue, "none", 0, 0, 0, 0, attr["tooltiptext"],
503                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
504                 m_panelStack.back() );
505         m_curLayer++;
506         m_pData->m_listSlider.push_back( slider );
507     }
508
509     else if( rName == "SliderBackground" )
510     {
511         RequireAttr( attr, rName, "image" );
512         DefaultAttr( attr, "nbhoriz", "1" );
513         DefaultAttr( attr, "nbvert", "1" );
514         DefaultAttr( attr, "padhoriz", "0" );
515         DefaultAttr( attr, "padvert", "0" );
516
517         // Retrieve the current slider data
518         BuilderData::Slider &slider = m_pData->m_listSlider.back();
519
520         slider.m_imageId = attr["image"];
521         slider.m_nbHoriz = atoi( attr["nbhoriz"] );
522         slider.m_nbVert = atoi( attr["nbvert"] );
523         slider.m_padHoriz = atoi( attr["padhoriz"] );
524         slider.m_padVert = atoi( attr["padvert"] );
525     }
526
527     else if( rName == "Text" )
528     {
529         RequireAttr( attr, rName, "font" );
530         DefaultAttr( attr, "id", "none" );
531         DefaultAttr( attr, "visible", "true" );
532         DefaultAttr( attr, "x", "0" );
533         DefaultAttr( attr, "y", "0" );
534         DefaultAttr( attr, "text", "" );
535         DefaultAttr( attr, "color", "#000000" );
536         DefaultAttr( attr, "scrolling", "auto" );
537         DefaultAttr( attr, "alignment", "left" );
538         DefaultAttr( attr, "width", "0" );
539         DefaultAttr( attr, "lefttop", "lefttop" );
540         DefaultAttr( attr, "rightbottom", "lefttop" );
541         DefaultAttr( attr, "xkeepratio", "false" );
542         DefaultAttr( attr, "ykeepratio", "false" );
543         DefaultAttr( attr, "help", "" );
544
545         const BuilderData::Text textData( uniqueId( attr["id"] ),
546                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
547                 attr["visible"], attr["font"],
548                 attr["text"], atoi( attr["width"] ),
549                 attr["lefttop"], attr["rightbottom"],
550                 convertBoolean( attr["xkeepratio"] ),
551                 convertBoolean( attr["ykeepratio"] ),
552                 convertColor( attr["color"] ),
553                 attr["scrolling"], attr["alignment"],
554                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
555                 m_panelStack.back() );
556         m_curLayer++;
557         m_pData->m_listText.push_back( textData );
558     }
559
560     else if( rName == "Theme" )
561     {
562         RequireAttr( attr, rName, "version" );
563         DefaultAttr( attr, "tooltipfont", "defaultfont" );
564         DefaultAttr( attr, "magnet", "15" );
565         DefaultAttr( attr, "alpha", "255" );
566         DefaultAttr( attr, "movealpha", "255" );
567
568         // Check the version
569         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
570         {
571             msg_Err( getIntf(), "bad theme version : %s (you need version %s)",
572                      attr["version"], SKINS_DTD_VERSION );
573             m_errors = true;
574             return;
575         }
576         const BuilderData::Theme theme( attr["tooltipfont"],
577                 atoi( attr["magnet"] ),
578                 convertInRange( attr["alpha"], 1, 255, "alpha" ),
579                 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
580         m_pData->m_listTheme.push_back( theme );
581     }
582
583     else if( rName == "ThemeInfo" )
584     {
585         DefaultAttr( attr, "name", "" );
586         DefaultAttr( attr, "author", "" );
587         DefaultAttr( attr, "email", "" );
588         DefaultAttr( attr, "website", "" );
589         msg_Info( getIntf(), "skin: %s  author: %s", attr["name"],
590                   attr["author"] );
591     }
592
593     else if( rName == "Video" )
594     {
595         DefaultAttr( attr, "id", "none" );
596         DefaultAttr( attr, "visible", "true" );
597         DefaultAttr( attr, "x", "0" );
598         DefaultAttr( attr, "y", "0" );
599         DefaultAttr( attr, "width", "0" );
600         DefaultAttr( attr, "height", "0" );
601         DefaultAttr( attr, "lefttop", "lefttop" );
602         DefaultAttr( attr, "rightbottom", "lefttop" );
603         DefaultAttr( attr, "xkeepratio", "false" );
604         DefaultAttr( attr, "ykeepratio", "false" );
605         DefaultAttr( attr, "autoresize", "false" );
606         DefaultAttr( attr, "help", "" );
607
608         const BuilderData::Video videoData( uniqueId( attr["id"] ),
609                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
610                 atoi( attr["width"] ), atoi( attr["height" ]),
611                 attr["lefttop"], attr["rightbottom"],
612                 convertBoolean( attr["xkeepratio"] ),
613                 convertBoolean( attr["ykeepratio"] ),
614                 attr["visible"], convertBoolean( attr["autoresize"] ),
615                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
616                 m_panelStack.back() );
617         m_curLayer++;
618         m_pData->m_listVideo.push_back( videoData );
619     }
620
621     else if( rName == "Window" )
622     {
623         DefaultAttr( attr, "id", "none" );
624         DefaultAttr( attr, "visible", "true" );
625         DefaultAttr( attr, "x", "0" );
626         DefaultAttr( attr, "y", "0" );
627         DefaultAttr( attr, "dragdrop", "true" );
628         DefaultAttr( attr, "playondrop", "true" );
629
630         m_curWindowId = uniqueId( attr["id"] );
631         const BuilderData::Window window( m_curWindowId,
632                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
633                 convertBoolean( attr["visible"] ),
634                 convertBoolean( attr["dragdrop"] ),
635                 convertBoolean( attr["playondrop"] ) );
636         m_pData->m_listWindow.push_back( window );
637     }
638 #undef  RequireAttr
639 }
640
641
642 void SkinParser::handleEndElement( const string &rName )
643 {
644     if( rName == "Group" )
645     {
646         m_xOffset -= m_xOffsetList.back();
647         m_yOffset -= m_yOffsetList.back();
648         m_xOffsetList.pop_back();
649         m_yOffsetList.pop_back();
650     }
651     else if( rName == "Playtree" || rName == "Playlist" )
652     {
653         m_curTreeId = "";
654     }
655     else if( rName == "Popup" )
656     {
657         m_curPopupId = "";
658         m_popupPosList.pop_back();
659     }
660     else if( rName == "Panel" )
661     {
662         m_panelStack.pop_back();
663     }
664 }
665
666
667 bool SkinParser::convertBoolean( const char *value ) const
668 {
669     return strcmp( value, "true" ) == 0;
670 }
671
672
673 int SkinParser::convertColor( const char *transcolor )
674 {
675     // TODO: move to the builder
676     unsigned long iRed, iGreen, iBlue;
677     iRed = iGreen = iBlue = 0;
678     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
679     return ( iRed << 16 | iGreen << 8 | iBlue );
680 }
681
682
683 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
684                                 const string &rAttribute ) const
685 {
686     int intValue = atoi( value );
687
688     if( intValue < minValue )
689     {
690         msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
691                   "expected range [%i, %i], using %i instead",
692                   rAttribute.c_str(), intValue, minValue, maxValue, minValue );
693         return minValue;
694     }
695     else if( intValue > maxValue )
696     {
697         msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
698                   "expected range [%i, %i], using %i instead",
699                   rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
700         return maxValue;
701     }
702     else
703     {
704         return intValue;
705     }
706 }
707
708
709 const string SkinParser::generateId() const
710 {
711     static int i = 1;
712
713     char genId[5];
714     snprintf( genId, 4, "%i", i++ );
715
716     string base = "_ReservedId_" + (string)genId;
717
718     return base;
719 }
720
721
722 const string SkinParser::uniqueId( const string &id )
723 {
724     string newId;
725
726     if( m_idSet.find( id ) != m_idSet.end() )
727     {
728         // The id was already used
729         if( id != "none" )
730         {
731             msg_Warn( getIntf(), "non-unique id: %s", id.c_str() );
732         }
733         newId = generateId();
734     }
735     else
736     {
737         // OK, this is a new id
738         newId = id;
739     }
740
741     // Add the id to the set
742     m_idSet.insert( newId );
743
744     return newId;
745 }