]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
f0937e4d9d5cd7a70eea17781020b613471928bc
[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, BuilderData *pData ):
31     XMLParser( pIntf, rFileName ), 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         SkinParser subParser( getIntf(), fullPath.c_str(), m_path, m_pData );
80         subParser.parse();
81     }
82
83     else if( rName == "IniFile" )
84     {
85         RequireAttr( attr, rName, "id" );
86         RequireAttr( attr, rName, "file" );
87
88         const BuilderData::IniFile iniFile( uniqueId( attr["id"] ),
89                 attr["file"] );
90         m_pData->m_listIniFile.push_back( iniFile );
91     }
92
93     else if( rName == "Anchor" )
94     {
95         RequireAttr( attr, rName, "priority" );
96         DefaultAttr( attr, "x", "0" );
97         DefaultAttr( attr, "y", "0" );
98         DefaultAttr( attr, "lefttop", "lefttop" );
99         DefaultAttr( attr, "points", "(0,0)" );
100         DefaultAttr( attr, "range", "10" );
101
102         int refWidth, refHeight;
103         getRefDimensions( refWidth, refHeight, false );
104         int x = getDimension( attr["x"], refWidth );
105         int y = getDimension( attr["y"], refHeight );
106         const BuilderData::Anchor anchor( x + m_xOffset,
107                 y + m_yOffset, attr["lefttop"],
108                 atoi( attr["range"] ), atoi( attr["priority"] ),
109                 attr["points"], m_curLayoutId );
110         m_pData->m_listAnchor.push_back( anchor );
111     }
112
113     else if( rName == "Bitmap" )
114     {
115         RequireAttr( attr, rName, "id" );
116         RequireAttr( attr, rName, "file" );
117         RequireAttr( attr, rName, "alphacolor" );
118         DefaultAttr( attr, "nbframes", "1" );
119         DefaultAttr( attr, "fps", "4" );
120         DefaultAttr( attr, "loop", "0" );
121
122         m_curBitmapId = uniqueId( attr["id"] );
123         const BuilderData::Bitmap bitmap( m_curBitmapId,
124                 attr["file"], convertColor( attr["alphacolor"] ),
125                 atoi( attr["nbframes"] ), atoi( attr["fps"] ),
126                 atoi( attr["loop"] ) );
127         m_pData->m_listBitmap.push_back( bitmap );
128     }
129
130     else if( rName == "SubBitmap" )
131     {
132         RequireAttr( attr, rName, "id" );
133         RequireAttr( attr, rName, "x" );
134         RequireAttr( attr, rName, "y" );
135         RequireAttr( attr, rName, "width" );
136         RequireAttr( attr, rName, "height" );
137         DefaultAttr( attr, "nbframes", "1" );
138         DefaultAttr( attr, "fps", "4" );
139         DefaultAttr( attr, "loop", "0" );
140
141         const BuilderData::SubBitmap bitmap( uniqueId( attr["id"] ),
142                 m_curBitmapId, atoi( attr["x"] ), atoi( attr["y"] ),
143                 atoi( attr["width"] ), atoi( attr["height"] ),
144                 atoi( attr["nbframes"] ), atoi( attr["fps"] ),
145                 atoi( attr["loop"] ) );
146         m_pData->m_listSubBitmap.push_back( bitmap );
147     }
148
149     else if( rName == "BitmapFont" )
150     {
151         RequireAttr( attr, rName, "id" );
152         RequireAttr( attr, rName, "file" );
153         DefaultAttr( attr, "type", "digits" );
154
155         const BuilderData::BitmapFont font( uniqueId( attr["id"] ),
156                 attr["file"], attr["type"] );
157         m_pData->m_listBitmapFont.push_back( font );
158     }
159
160     else if( rName == "PopupMenu" )
161     {
162         RequireAttr( attr, rName, "id" );
163
164         m_popupPosList.push_back(0);
165         m_curPopupId = uniqueId( attr["id"] );
166         const BuilderData::PopupMenu popup( m_curPopupId );
167         m_pData->m_listPopupMenu.push_back( popup );
168     }
169
170     else if( rName == "MenuItem" )
171     {
172         RequireAttr( attr, rName, "label" );
173         DefaultAttr( attr, "action", "none" );
174
175         const BuilderData::MenuItem item( attr["label"], attr["action"],
176                                           m_popupPosList.back(),
177                                           m_curPopupId );
178         m_pData->m_listMenuItem.push_back( item );
179         m_popupPosList.back()++;
180     }
181
182     else if( rName == "MenuSeparator" )
183     {
184         const BuilderData::MenuSeparator sep( m_popupPosList.back(),
185                                               m_curPopupId );
186         m_pData->m_listMenuSeparator.push_back( sep );
187         m_popupPosList.back()++;
188     }
189
190     else if( rName == "Button" )
191     {
192         RequireAttr( attr, rName, "up" );
193         DefaultAttr( attr, "id", "none" );
194         DefaultAttr( attr, "visible", "true" );
195         DefaultAttr( attr, "x", "0" );
196         DefaultAttr( attr, "y", "0" );
197         DefaultAttr( attr, "lefttop", "lefttop" );
198         DefaultAttr( attr, "rightbottom", "lefttop" );
199         DefaultAttr( attr, "xkeepratio", "false" );
200         DefaultAttr( attr, "ykeepratio", "false" );
201         DefaultAttr( attr, "down", "none" );
202         DefaultAttr( attr, "over", "none" );
203         DefaultAttr( attr, "action", "none" );
204         DefaultAttr( attr, "tooltiptext", "" );
205         DefaultAttr( attr, "help", "" );
206
207         int refWidth, refHeight;
208         getRefDimensions( refWidth, refHeight, false );
209         int x = getDimension( attr["x"], refWidth );
210         int y = getDimension( attr["y"], refHeight );
211         const BuilderData::Button button( uniqueId( attr["id"] ),
212                 x + m_xOffset, y + m_yOffset,
213                 attr["lefttop"], attr["rightbottom"],
214                 convertBoolean( attr["xkeepratio"] ),
215                 convertBoolean( attr["ykeepratio"] ), attr["visible"],
216                 attr["up"], attr["down"], attr["over"], attr["action"],
217                 attr["tooltiptext"], attr["help"],
218                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
219         m_curLayer++;
220         m_pData->m_listButton.push_back( button );
221     }
222
223     else if( rName == "Checkbox" )
224     {
225         RequireAttr( attr, rName, "up1" );
226         RequireAttr( attr, rName, "up2" );
227         RequireAttr( attr, rName, "state" );
228         DefaultAttr( attr, "id", "none" );
229         DefaultAttr( attr, "visible", "true" );
230         DefaultAttr( attr, "x", "0" );
231         DefaultAttr( attr, "y", "0" );
232         DefaultAttr( attr, "lefttop", "lefttop" );
233         DefaultAttr( attr, "rightbottom", "lefttop" );
234         DefaultAttr( attr, "xkeepratio", "false" );
235         DefaultAttr( attr, "ykeepratio", "false" );
236         DefaultAttr( attr, "down1", "none" );
237         DefaultAttr( attr, "over1", "none" );
238         DefaultAttr( attr, "down2", "none" );
239         DefaultAttr( attr, "over2", "none" );
240         DefaultAttr( attr, "action1", "none" );
241         DefaultAttr( attr, "action2", "none" );
242         DefaultAttr( attr, "tooltiptext1", "" );
243         DefaultAttr( attr, "tooltiptext2", "" );
244         DefaultAttr( attr, "help", "" );
245
246         int refWidth, refHeight;
247         getRefDimensions( refWidth, refHeight, false );
248         int x = getDimension( attr["x"], refWidth );
249         int y = getDimension( attr["y"], refHeight );
250         const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
251                 x + m_xOffset, y + m_yOffset,
252                 attr["lefttop"], attr["rightbottom"],
253                 convertBoolean( attr["xkeepratio"] ),
254                 convertBoolean( attr["ykeepratio"] ), attr["visible"],
255                 attr["up1"], attr["down1"], attr["over1"],
256                 attr["up2"], attr["down2"], attr["over2"], attr["state"],
257                 attr["action1"], attr["action2"], attr["tooltiptext1"],
258                 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
259                 m_curLayoutId, m_panelStack.back() );
260         m_curLayer++;
261         m_pData->m_listCheckbox.push_back( checkbox );
262     }
263
264     else if( rName == "Font" )
265     {
266         RequireAttr( attr, rName, "id" );
267         RequireAttr( attr, rName, "file" );
268         DefaultAttr( attr, "size", "12" );
269
270         const BuilderData::Font fontData( uniqueId( attr["id"] ),
271                 attr["file"], atoi( attr["size"] ) );
272         m_pData->m_listFont.push_back( fontData );
273     }
274
275     else if( rName == "Group" )
276     {
277         DefaultAttr( attr, "x", "0" );
278         DefaultAttr( attr, "y", "0" );
279
280         m_xOffset += atoi( attr["x"] );
281         m_yOffset += atoi( attr["y"] );
282         m_xOffsetList.push_back( atoi( attr["x"] ) );
283         m_yOffsetList.push_back( atoi( attr["y"] ) );
284     }
285
286     else if( rName == "Image" )
287     {
288         RequireAttr( attr, rName, "image" );
289         DefaultAttr( attr, "id", "none" );
290         DefaultAttr( attr, "visible", "true" );
291         DefaultAttr( attr, "x", "0" );
292         DefaultAttr( attr, "y", "0" );
293         DefaultAttr( attr, "width", "-1" );
294         DefaultAttr( attr, "height", "-1" );
295         DefaultAttr( attr, "lefttop", "lefttop" );
296         DefaultAttr( attr, "rightbottom", "lefttop" );
297         DefaultAttr( attr, "xkeepratio", "false" );
298         DefaultAttr( attr, "ykeepratio", "false" );
299         DefaultAttr( attr, "action", "none" );
300         DefaultAttr( attr, "action2", "none" );
301         DefaultAttr( attr, "resize", "mosaic" );
302         DefaultAttr( attr, "help", "" );
303         DefaultAttr( attr, "art", "false" );
304
305         int refWidth, refHeight;
306         getRefDimensions( refWidth, refHeight, false );
307         int x = getDimension( attr["x"], refWidth );
308         int y = getDimension( attr["y"], refHeight );
309         int width = getDimension( attr["width"], refWidth );
310         int height = getDimension( attr["height"], refHeight );
311         const BuilderData::Image imageData( uniqueId( attr["id"] ),
312                 x + m_xOffset, y + m_yOffset, width, height,
313                 attr["lefttop"], attr["rightbottom"],
314                 convertBoolean( attr["xkeepratio"] ),
315                 convertBoolean( attr["ykeepratio"] ), attr["visible"],
316                 attr["image"], attr["action"], attr["action2"], attr["resize"],
317                 attr["help"], convertBoolean( attr["art"] ),
318                 m_curLayer, m_curWindowId, m_curLayoutId,
319                 m_panelStack.back() );
320         m_curLayer++;
321         m_pData->m_listImage.push_back( imageData );
322     }
323
324     else if( rName == "Layout" )
325     {
326         RequireAttr( attr, rName, "width" );
327         RequireAttr( attr, rName, "height" );
328         DefaultAttr( attr, "id", "none" );
329         DefaultAttr( attr, "minwidth", "-1" );
330         DefaultAttr( attr, "maxwidth", "-1" );
331         DefaultAttr( attr, "minheight", "-1" );
332         DefaultAttr( attr, "maxheight", "-1" );
333
334         int refWidth, refHeight;
335         getRefDimensions( refWidth, refHeight, true );
336         int width = getDimension( attr["width"], refWidth );
337         int height = getDimension( attr["height"], refHeight );
338
339         m_curLayoutId = uniqueId( attr["id"] );
340         const BuilderData::Layout layout( m_curLayoutId,
341                 width, height,
342                 getDimension( attr["minwidth"], refWidth ),
343                 getDimension( attr["maxwidth"], refWidth ),
344                 getDimension( attr["minheight"], refHeight ),
345                 getDimension( attr["maxheight"], refHeight ),
346                 m_curWindowId );
347
348         updateWindowPos( width, height );
349         m_pData->m_listLayout.push_back( layout );
350         m_curLayer = 0;
351     }
352
353     else if( rName == "Panel" )
354     {
355         DefaultAttr( attr, "x", "0" );
356         DefaultAttr( attr, "y", "0" );
357         DefaultAttr( attr, "lefttop", "lefttop" );
358         DefaultAttr( attr, "rightbottom", "lefttop" );
359         DefaultAttr( attr, "xkeepratio", "false" );
360         DefaultAttr( attr, "ykeepratio", "false" );
361         RequireAttr( attr, rName, "width" );
362         RequireAttr( attr, rName, "height" );
363         DefaultAttr( attr, "position", "-1" );
364         DefaultAttr( attr, "xoffset", "0" );
365         DefaultAttr( attr, "yoffset", "0" );
366         DefaultAttr( attr, "xmargin", "0" );
367         DefaultAttr( attr, "ymargin", "0" );
368
369         int refWidth, refHeight;
370         getRefDimensions( refWidth, refHeight, false );
371         int x = getDimension( attr["x"], refWidth );
372         int y = getDimension( attr["y"], refHeight );
373         int width = getDimension( attr["width"], refWidth );
374         int height = getDimension( attr["height"], refHeight );
375         convertPosition( attr["position"],
376                          attr["xoffset"], attr["yoffset"],
377                          attr["xmargin"], attr["ymargin"],
378                          width, height, refWidth, refHeight, &x, &y );
379
380         string panelId = uniqueId( "none" );
381         const BuilderData::Panel panel( panelId,
382                 x + m_xOffset, y + m_yOffset,
383                 attr["lefttop"], attr["rightbottom"],
384                 convertBoolean( attr["xkeepratio"] ),
385                 convertBoolean( attr["ykeepratio"] ),
386                 width, height,
387                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
388         m_curLayer++;
389         m_pData->m_listPanel.push_back( panel );
390         // Add the panel to the stack
391         m_panelStack.push_back( panelId );
392     }
393
394     else if( rName == "Playlist" )
395     {
396         RequireAttr( attr, rName, "id" );
397         RequireAttr( attr, rName, "font" );
398         DefaultAttr( attr, "visible", "true" );
399         DefaultAttr( attr, "flat", "true" ); // Only difference here
400         DefaultAttr( attr, "x", "0" );
401         DefaultAttr( attr, "y", "0" );
402         DefaultAttr( attr, "width", "0" );
403         DefaultAttr( attr, "height", "0" );
404         DefaultAttr( attr, "position", "-1" );
405         DefaultAttr( attr, "xoffset", "0" );
406         DefaultAttr( attr, "yoffset", "0" );
407         DefaultAttr( attr, "xmargin", "0" );
408         DefaultAttr( attr, "ymargin", "0" );
409         DefaultAttr( attr, "lefttop", "lefttop" );
410         DefaultAttr( attr, "rightbottom", "lefttop" );
411         DefaultAttr( attr, "xkeepratio", "false" );
412         DefaultAttr( attr, "ykeepratio", "false" );
413         DefaultAttr( attr, "bgimage", "none" );
414         DefaultAttr( attr, "itemimage", "none" );
415         DefaultAttr( attr, "openimage", "none" );
416         DefaultAttr( attr, "closedimage", "none" );
417         DefaultAttr( attr, "fgcolor", "#000000" );
418         DefaultAttr( attr, "playcolor", "#FF0000" );
419         DefaultAttr( attr, "bgcolor1", "#FFFFFF" );
420         DefaultAttr( attr, "bgcolor2", "#FFFFFF" );
421         DefaultAttr( attr, "selcolor", "#0000FF" );
422         DefaultAttr( attr, "help", "" );
423
424         int refWidth, refHeight;
425         getRefDimensions( refWidth, refHeight, false );
426         int x = getDimension( attr["x"], refWidth );
427         int y = getDimension( attr["y"], refHeight );
428         int width = getDimension( attr["width"], refWidth );
429         int height = getDimension( attr["height"], refHeight );
430         convertPosition( attr["position"],
431                          attr["xoffset"], attr["yoffset"],
432                          attr["xmargin"], attr["ymargin"],
433                          width, height, refWidth, refHeight, &x, &y );
434
435         m_curTreeId = uniqueId( attr["id"] );
436         const BuilderData::Tree treeData( m_curTreeId,
437                 x + m_xOffset, y + m_yOffset, attr["visible"],
438                 attr["flat"],
439                 width, height,
440                 attr["lefttop"], attr["rightbottom"],
441                 convertBoolean( attr["xkeepratio"] ),
442                 convertBoolean( attr["ykeepratio"] ),
443                 attr["font"], "playtree",
444                 attr["bgimage"], attr["itemimage"],
445                 attr["openimage"], attr["closedimage"],
446                 attr["fgcolor"],
447                 attr["playcolor"],
448                 attr["bgcolor1"],
449                 attr["bgcolor2"],
450                 attr["selcolor"], attr["help"],
451                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
452         m_curLayer++;
453         m_pData->m_listTree.push_back( treeData );
454     }
455     else if( rName == "Playtree" )
456     {
457         RequireAttr( attr, rName, "id" );
458         RequireAttr( attr, rName, "font" );
459         DefaultAttr( attr, "visible", "true" );
460         DefaultAttr( attr, "flat", "false" );
461         DefaultAttr( attr, "x", "0" );
462         DefaultAttr( attr, "y", "0" );
463         DefaultAttr( attr, "width", "0" );
464         DefaultAttr( attr, "height", "0" );
465         DefaultAttr( attr, "position", "-1" );
466         DefaultAttr( attr, "xoffset", "0" );
467         DefaultAttr( attr, "yoffset", "0" );
468         DefaultAttr( attr, "xmargin", "0" );
469         DefaultAttr( attr, "ymargin", "0" );
470         DefaultAttr( attr, "lefttop", "lefttop" );
471         DefaultAttr( attr, "rightbottom", "lefttop" );
472         DefaultAttr( attr, "xkeepratio", "false" );
473         DefaultAttr( attr, "ykeepratio", "false" );
474         DefaultAttr( attr, "bgimage", "none" );
475         DefaultAttr( attr, "itemimage", "none" );
476         DefaultAttr( attr, "openimage", "none" );
477         DefaultAttr( attr, "closedimage", "none" );
478         DefaultAttr( attr, "fgcolor", "#000000" );
479         DefaultAttr( attr, "playcolor", "#FF0000" );
480         DefaultAttr( attr, "bgcolor1", "#FFFFFF" );
481         DefaultAttr( attr, "bgcolor2", "#FFFFFF" );
482         DefaultAttr( attr, "selcolor", "#0000FF" );
483         DefaultAttr( attr, "help", "" );
484
485         int refWidth, refHeight;
486         getRefDimensions( refWidth, refHeight, false );
487         int x = getDimension( attr["x"], refWidth );
488         int y = getDimension( attr["y"], refHeight );
489         int width = getDimension( attr["width"], refWidth );
490         int height = getDimension( attr["height"], refHeight );
491         convertPosition( attr["position"],
492                          attr["xoffset"], attr["yoffset"],
493                          attr["xmargin"], attr["ymargin"],
494                          width, height, refWidth, refHeight, &x, &y );
495
496         m_curTreeId = uniqueId( attr["id"] );
497         const BuilderData::Tree treeData( m_curTreeId,
498                 x + m_xOffset, y + m_yOffset, attr["visible"],
499                 attr["flat"],
500                 width, height,
501                 attr["lefttop"], attr["rightbottom"],
502                 convertBoolean( attr["xkeepratio"] ),
503                 convertBoolean( attr["ykeepratio"] ),
504                 attr["font"], "playtree",
505                 attr["bgimage"], attr["itemimage"],
506                 attr["openimage"], attr["closedimage"],
507                 attr["fgcolor"], attr["playcolor"],
508                 attr["bgcolor1"], attr["bgcolor2"],
509                 attr["selcolor"], attr["help"],
510                 m_curLayer, m_curWindowId, m_curLayoutId, m_panelStack.back() );
511         m_curLayer++;
512         m_pData->m_listTree.push_back( treeData );
513     }
514
515     else if( rName == "RadialSlider" )
516     {
517         RequireAttr( attr, rName, "sequence" );
518         RequireAttr( attr, rName, "nbimages" );
519         DefaultAttr( attr, "id", "none" );
520         DefaultAttr( attr, "visible", "true" );
521         DefaultAttr( attr, "x", "0" );
522         DefaultAttr( attr, "y", "0" );
523         DefaultAttr( attr, "lefttop", "lefttop" );
524         DefaultAttr( attr, "rightbottom", "lefttop" );
525         DefaultAttr( attr, "xkeepratio", "false" );
526         DefaultAttr( attr, "ykeepratio", "false" );
527         DefaultAttr( attr, "minangle", "0" );
528         DefaultAttr( attr, "maxangle", "360" );
529         DefaultAttr( attr, "value", "none" );
530         DefaultAttr( attr, "tooltiptext", "" );
531         DefaultAttr( attr, "help", "" );
532
533         int refWidth, refHeight;
534         getRefDimensions( refWidth, refHeight, false );
535         int x = getDimension( attr["x"], refWidth );
536         int y = getDimension( attr["y"], refHeight );
537         const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
538                 attr["visible"],
539                 x + m_xOffset, y + m_yOffset,
540                 attr["lefttop"], attr["rightbottom"],
541                 convertBoolean( attr["xkeepratio"] ),
542                 convertBoolean( attr["ykeepratio"] ), attr["sequence"],
543                 atoi( attr["nbimages"] ), atof( attr["minangle"] ) * M_PI /180,
544                 atof( attr["maxangle"] ) * M_PI / 180, attr["value"],
545                 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
546                 m_curLayoutId, m_panelStack.back() );
547         m_curLayer++;
548         m_pData->m_listRadialSlider.push_back( radial );
549     }
550
551     else if( rName == "Slider" )
552     {
553         RequireAttr( attr, rName, "up" );
554         RequireAttr( attr, rName, "points" );
555         DefaultAttr( attr, "id", "none" );
556         DefaultAttr( attr, "visible", "true" );
557         DefaultAttr( attr, "x", "0" );
558         DefaultAttr( attr, "y", "0" );
559         DefaultAttr( attr, "lefttop", "lefttop" );
560         DefaultAttr( attr, "rightbottom", "lefttop" );
561         DefaultAttr( attr, "xkeepratio", "false" );
562         DefaultAttr( attr, "ykeepratio", "false" );
563         DefaultAttr( attr, "down", "none" );
564         DefaultAttr( attr, "over", "none" );
565         DefaultAttr( attr, "thickness", "10" );
566         DefaultAttr( attr, "value", "none" );
567         DefaultAttr( attr, "tooltiptext", "" );
568         DefaultAttr( attr, "help", "" );
569
570         string newValue = attr["value"];
571         if( m_curTreeId != "" )
572         {
573             // Slider associated to a tree
574             newValue = "playtree.slider";
575         }
576
577         int refWidth, refHeight;
578         getRefDimensions( refWidth, refHeight, false );
579         int x = getDimension( attr["x"], refWidth );
580         int y = getDimension( attr["y"], refHeight );
581         const BuilderData::Slider slider( uniqueId( attr["id"] ),
582                 attr["visible"], x + m_xOffset,
583                 y + m_yOffset, attr["lefttop"],
584                 attr["rightbottom"], convertBoolean( attr["xkeepratio"] ),
585                 convertBoolean( attr["ykeepratio"] ), attr["up"], attr["down"],
586                 attr["over"], attr["points"], atoi( attr["thickness"] ),
587                 newValue, "none", 0, 0, 0, 0, attr["tooltiptext"],
588                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
589                 m_panelStack.back() );
590         m_curLayer++;
591         m_pData->m_listSlider.push_back( slider );
592     }
593
594     else if( rName == "SliderBackground" )
595     {
596         RequireAttr( attr, rName, "image" );
597         DefaultAttr( attr, "nbhoriz", "1" );
598         DefaultAttr( attr, "nbvert", "1" );
599         DefaultAttr( attr, "padhoriz", "0" );
600         DefaultAttr( attr, "padvert", "0" );
601
602         // Retrieve the current slider data
603         BuilderData::Slider &slider = m_pData->m_listSlider.back();
604
605         slider.m_imageId = attr["image"];
606         slider.m_nbHoriz = atoi( attr["nbhoriz"] );
607         slider.m_nbVert = atoi( attr["nbvert"] );
608         slider.m_padHoriz = atoi( attr["padhoriz"] );
609         slider.m_padVert = atoi( attr["padvert"] );
610     }
611
612     else if( rName == "Text" )
613     {
614         RequireAttr( attr, rName, "font" );
615         DefaultAttr( attr, "id", "none" );
616         DefaultAttr( attr, "visible", "true" );
617         DefaultAttr( attr, "x", "0" );
618         DefaultAttr( attr, "y", "0" );
619         DefaultAttr( attr, "text", "" );
620         DefaultAttr( attr, "color", "#000000" );
621         DefaultAttr( attr, "scrolling", "auto" );
622         DefaultAttr( attr, "alignment", "left" );
623         DefaultAttr( attr, "focus", "true" );
624         DefaultAttr( attr, "width", "0" );
625         DefaultAttr( attr, "lefttop", "lefttop" );
626         DefaultAttr( attr, "rightbottom", "lefttop" );
627         DefaultAttr( attr, "xkeepratio", "false" );
628         DefaultAttr( attr, "ykeepratio", "false" );
629         DefaultAttr( attr, "help", "" );
630
631         int refWidth, refHeight;
632         getRefDimensions( refWidth, refHeight, false );
633         int x = getDimension( attr["x"], refWidth );
634         int y = getDimension( attr["y"], refHeight );
635         int width = getDimension( attr["width"], refWidth );
636
637         const BuilderData::Text textData( uniqueId( attr["id"] ),
638                 x + m_xOffset, y + m_yOffset,
639                 attr["visible"], attr["font"],
640                 attr["text"],
641                 width,
642                 attr["lefttop"], attr["rightbottom"],
643                 convertBoolean( attr["xkeepratio"] ),
644                 convertBoolean( attr["ykeepratio"] ),
645                 convertColor( attr["color"] ),
646                 attr["scrolling"], attr["alignment"],
647                 attr["focus"], attr["help"],
648                 m_curLayer, m_curWindowId, m_curLayoutId,
649                 m_panelStack.back() );
650         m_curLayer++;
651         m_pData->m_listText.push_back( textData );
652     }
653
654     else if( rName == "Theme" )
655     {
656         RequireAttr( attr, rName, "version" );
657         DefaultAttr( attr, "tooltipfont", "defaultfont" );
658         DefaultAttr( attr, "magnet", "15" );
659         DefaultAttr( attr, "alpha", "255" );
660         DefaultAttr( attr, "movealpha", "255" );
661
662         // Check the version
663         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
664         {
665             msg_Err( getIntf(), "bad theme version : %s (you need version %s)",
666                      attr["version"], SKINS_DTD_VERSION );
667             m_errors = true;
668             return;
669         }
670         const BuilderData::Theme theme( attr["tooltipfont"],
671                 atoi( attr["magnet"] ),
672                 convertInRange( attr["alpha"], 1, 255, "alpha" ),
673                 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
674         m_pData->m_listTheme.push_back( theme );
675     }
676
677     else if( rName == "ThemeInfo" )
678     {
679         DefaultAttr( attr, "name", "" );
680         DefaultAttr( attr, "author", "" );
681         DefaultAttr( attr, "email", "" );
682         DefaultAttr( attr, "website", "" );
683         msg_Info( getIntf(), "skin: %s  author: %s", attr["name"],
684                   attr["author"] );
685     }
686
687     else if( rName == "Video" )
688     {
689         DefaultAttr( attr, "id", "none" );
690         DefaultAttr( attr, "visible", "true" );
691         DefaultAttr( attr, "x", "0" );
692         DefaultAttr( attr, "y", "0" );
693         DefaultAttr( attr, "width", "0" );
694         DefaultAttr( attr, "height", "0" );
695         DefaultAttr( attr, "position", "-1" );
696         DefaultAttr( attr, "xoffset", "0" );
697         DefaultAttr( attr, "yoffset", "0" );
698         DefaultAttr( attr, "xmargin", "0" );
699         DefaultAttr( attr, "ymargin", "0" );
700         DefaultAttr( attr, "lefttop", "lefttop" );
701         DefaultAttr( attr, "rightbottom", "lefttop" );
702         DefaultAttr( attr, "xkeepratio", "false" );
703         DefaultAttr( attr, "ykeepratio", "false" );
704         DefaultAttr( attr, "autoresize", "false" );
705         DefaultAttr( attr, "help", "" );
706
707         int refWidth, refHeight;
708         getRefDimensions( refWidth, refHeight, false );
709         int x = getDimension( attr["x"], refWidth );
710         int y = getDimension( attr["y"], refHeight );
711         int width = getDimension( attr["width"], refWidth );
712         int height = getDimension( attr["height"], refHeight );
713         convertPosition( attr["position"],
714                          attr["xoffset"], attr["yoffset"],
715                          attr["xmargin"], attr["ymargin"],
716                          width, height, refWidth, refHeight, &x, &y );
717
718         const BuilderData::Video videoData( uniqueId( attr["id"] ),
719                 x + m_xOffset, y + m_yOffset, width, height,
720                 attr["lefttop"], attr["rightbottom"],
721                 convertBoolean( attr["xkeepratio"] ),
722                 convertBoolean( attr["ykeepratio"] ),
723                 attr["visible"], convertBoolean( attr["autoresize"] ),
724                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId,
725                 m_panelStack.back() );
726         m_curLayer++;
727         m_pData->m_listVideo.push_back( videoData );
728     }
729
730     else if( rName == "Window" )
731     {
732         DefaultAttr( attr, "id", "none" );
733         DefaultAttr( attr, "visible", "true" );
734         DefaultAttr( attr, "x", "0" );
735         DefaultAttr( attr, "y", "0" );
736         DefaultAttr( attr, "position", "-1" );
737         DefaultAttr( attr, "xoffset", "0" );
738         DefaultAttr( attr, "yoffset", "0" );
739         DefaultAttr( attr, "xmargin", "0" );
740         DefaultAttr( attr, "ymargin", "0" );
741         DefaultAttr( attr, "dragdrop", "true" );
742         DefaultAttr( attr, "playondrop", "true" );
743
744         m_curWindowId = uniqueId( attr["id"] );
745
746         int refWidth, refHeight;
747         getRefDimensions( refWidth, refHeight, true );
748         int x = getDimension( attr["x"], refWidth );
749         int y = getDimension( attr["y"], refHeight );
750         const BuilderData::Window window( m_curWindowId,
751                 x + m_xOffset, y + m_yOffset,
752                 attr["position"],
753                 attr["xoffset"], attr["yoffset"],
754                 attr["xmargin"], attr["ymargin"],
755                 convertBoolean( attr["visible"] ),
756                 convertBoolean( attr["dragdrop"] ),
757                 convertBoolean( attr["playondrop"] ) );
758         m_pData->m_listWindow.push_back( window );
759     }
760 #undef  RequireAttr
761 }
762
763
764 void SkinParser::handleEndElement( const string &rName )
765 {
766     if( rName == "Group" )
767     {
768         m_xOffset -= m_xOffsetList.back();
769         m_yOffset -= m_yOffsetList.back();
770         m_xOffsetList.pop_back();
771         m_yOffsetList.pop_back();
772     }
773     else if( rName == "Playtree" || rName == "Playlist" )
774     {
775         m_curTreeId = "";
776     }
777     else if( rName == "Popup" )
778     {
779         m_curPopupId = "";
780         m_popupPosList.pop_back();
781     }
782     else if( rName == "Panel" )
783     {
784         m_panelStack.pop_back();
785     }
786 }
787
788
789 bool SkinParser::convertBoolean( const char *value ) const
790 {
791     return strcmp( value, "true" ) == 0;
792 }
793
794
795 int SkinParser::convertColor( const char *transcolor )
796 {
797     // TODO: move to the builder
798     unsigned long iRed, iGreen, iBlue;
799     iRed = iGreen = iBlue = 0;
800     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
801     return ( iRed << 16 | iGreen << 8 | iBlue );
802 }
803
804
805 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
806                                 const string &rAttribute ) const
807 {
808     int intValue = atoi( value );
809
810     if( intValue < minValue )
811     {
812         msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
813                   "expected range [%i, %i], using %i instead",
814                   rAttribute.c_str(), intValue, minValue, maxValue, minValue );
815         return minValue;
816     }
817     else if( intValue > maxValue )
818     {
819         msg_Warn( getIntf(), "value of \"%s\" attribute (%i) is out of the "
820                   "expected range [%i, %i], using %i instead",
821                   rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
822         return maxValue;
823     }
824     else
825     {
826         return intValue;
827     }
828 }
829
830
831 const string SkinParser::generateId() const
832 {
833     static int i = 1;
834
835     char genId[5];
836     snprintf( genId, 4, "%i", i++ );
837
838     string base = "_ReservedId_" + (string)genId;
839
840     return base;
841 }
842
843
844 const string SkinParser::uniqueId( const string &id )
845 {
846     string newId;
847
848     if( m_idSet.find( id ) != m_idSet.end() )
849     {
850         // The id was already used
851         if( id != "none" )
852         {
853             msg_Warn( getIntf(), "non-unique id: %s", id.c_str() );
854         }
855         newId = generateId();
856     }
857     else
858     {
859         // OK, this is a new id
860         newId = id;
861     }
862
863     // Add the id to the set
864     m_idSet.insert( newId );
865
866     return newId;
867 }
868
869 void SkinParser::getRefDimensions( int &rWidth, int &rHeight, bool toScreen )
870 {
871     if( toScreen )
872     {
873         OSFactory *pOsFactory = OSFactory::instance( getIntf() );
874         rWidth = pOsFactory->getScreenWidth();
875         rHeight = pOsFactory->getScreenHeight();
876         return;
877     }
878
879     string panelId = m_panelStack.back();
880     if( panelId != "none" )
881     {
882         list<BuilderData::Panel>::const_iterator it;
883         for( it = m_pData->m_listPanel.begin();
884              it != m_pData->m_listPanel.end(); ++it )
885         {
886             if( it->m_id == panelId )
887             {
888                 rWidth = it->m_width;
889                 rHeight = it->m_height;
890                 return;
891             }
892         }
893     }
894     else
895     {
896         const BuilderData::Layout layout = m_pData->m_listLayout.back();
897         rWidth = layout.m_width;
898         rHeight = layout.m_height;
899         return;
900     }
901     msg_Err( getIntf(), "failure to retrieve parent panel or layout" );
902 }
903
904
905 int SkinParser::getDimension( string value, int refDimension )
906 {
907     string::size_type leftPos;
908
909     leftPos = value.find( "%" );
910     if( leftPos != string::npos )
911     {
912         int val = atoi( value.substr( 0, leftPos ).c_str() );
913         return  val * refDimension / 100;
914     }
915
916     leftPos = value.find( "px" );
917     if( leftPos != string::npos )
918     {
919         int val = atoi( value.substr( 0, leftPos ).c_str() );
920         return val;
921     }
922
923     return atoi( value.c_str() );
924 }
925
926
927 int SkinParser::getPosition( string position )
928 {
929     if( position == "-1" )
930         return POS_UNDEF;
931     else if( position == "Center" )
932         return POS_CENTER;
933     else if( position == "North" )
934         return POS_TOP;
935     else if( position == "South" )
936         return POS_BOTTOM;
937     else if( position == "West" )
938         return POS_LEFT;
939     else if( position == "East" )
940         return POS_RIGHT;
941     else if( position == "NorthWest" )
942         return POS_TOP | POS_LEFT;
943     else if( position == "NorthEast" )
944         return POS_TOP | POS_RIGHT;
945     else if( position == "SouthWest" )
946         return POS_BOTTOM | POS_LEFT;
947     else if( position == "SouthEast" )
948         return POS_BOTTOM | POS_RIGHT;
949
950     msg_Err( getIntf(), "unknown value '%s' for position",
951                           position.c_str() );
952     return POS_UNDEF;
953 }
954
955
956 void SkinParser::convertPosition( string position, string xOffset,
957                           string yOffset, string xMargin,
958                           string yMargin, int width, int height,
959                           int refWidth, int refHeight, int* p_x, int* p_y )
960 {
961     int iPosition = getPosition( position );
962     if( iPosition == POS_UNDEF )
963         return;
964
965     int i_xOffset = getDimension( xOffset, refWidth );
966     int i_yOffset = getDimension( yOffset, refHeight );
967     int i_xMargin = getDimension( xMargin, refWidth );
968     int i_yMargin = getDimension( yMargin, refHeight );
969
970    // compute *p_x
971    if( iPosition & POS_LEFT )
972        *p_x = i_xMargin;
973    else if( iPosition & POS_RIGHT )
974        *p_x = refWidth - width - i_xMargin;
975    else
976        *p_x = ( refWidth - width ) / 2;
977
978    // compute *p_y
979    if( iPosition & POS_TOP )
980        *p_y = i_yMargin;
981    else if( iPosition & POS_BOTTOM )
982        *p_y = refHeight - height - i_yMargin;
983    else
984        *p_y = ( refHeight - height ) / 2;
985
986     // add offset
987     *p_x += i_xOffset;
988     *p_y += i_yOffset;
989 }
990
991
992 void SkinParser::updateWindowPos( int width, int height )
993 {
994     BuilderData::Window win = m_pData->m_listWindow.back();
995     m_pData->m_listWindow.pop_back();
996
997     int refWidth, refHeight;
998     getRefDimensions( refWidth, refHeight, true );
999     convertPosition( win.m_position,
1000                      win.m_xOffset, win.m_yOffset,
1001                      win.m_xMargin, win.m_yMargin,
1002                      width, height, refWidth, refHeight,
1003                      &win.m_xPos, &win.m_yPos );
1004
1005     m_pData->m_listWindow.push_back( win );
1006 }