]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
* all: the skin sliders can now have a background image, which
[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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "skin_parser.hpp"
25 #include "../src/os_factory.hpp"
26 #include <math.h>
27
28 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName,
29                         const string &rPath ):
30     XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 ),
31     m_path( rPath )
32 {
33 }
34
35
36 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
37 {
38 #define CheckDefault( a, b ) \
39     if( attr.find(a) == attr.end() ) attr[strdup(a)] = strdup(b);
40 #define RequireDefault( a ) \
41     if( attr.find(a) == attr.end() ) \
42     { \
43         msg_Err( getIntf(), "Bad theme (element: %s, missing attribute: %s)", \
44                  rName.c_str(), a ); \
45         m_errors = true; return; \
46     }
47
48     if( rName == "Anchor" )
49     {
50         RequireDefault( "priority" );
51         CheckDefault( "x", "0" );
52         CheckDefault( "y", "0" );
53         CheckDefault( "points", "(0,0)" );
54         CheckDefault( "range", "10" );
55
56         const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
57                 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
58                 atoi( attr["priority"] ), attr["points"], m_curLayoutId );
59         m_data.m_listAnchor.push_back( anchor );
60     }
61
62     else if( rName == "Bitmap" )
63     {
64         RequireDefault( "id" );
65         RequireDefault( "file" );
66         RequireDefault( "alphacolor" );
67
68         m_curBitmapId = uniqueId( attr["id"] );
69         const BuilderData::Bitmap bitmap( m_curBitmapId,
70                 convertFileName( attr["file"] ),
71                 convertColor( attr["alphacolor"] ) );
72         m_data.m_listBitmap.push_back( bitmap );
73     }
74
75     else if( rName == "SubBitmap" )
76     {
77         RequireDefault( "id" );
78         RequireDefault( "x" );
79         RequireDefault( "y" );
80         RequireDefault( "width" );
81         RequireDefault( "height" );
82
83         const BuilderData::SubBitmap bitmap( uniqueId( attr["id"] ),
84                 m_curBitmapId, atoi( attr["x"] ), atoi( attr["y"] ),
85                 atoi( attr["width"] ), atoi( attr["height"] ) );
86         m_data.m_listSubBitmap.push_back( bitmap );
87     }
88
89     else if( rName == "BitmapFont" )
90     {
91         RequireDefault( "id" );
92         RequireDefault( "file" );
93         CheckDefault( "type", "digits" );
94
95         const BuilderData::BitmapFont font( attr["id"],
96                 convertFileName( attr["file"] ),
97                 attr["type"] );
98         m_data.m_listBitmapFont.push_back( font );
99     }
100
101     else if( rName == "Button" )
102     {
103         RequireDefault( "up" );
104         CheckDefault( "id", "none" );
105         CheckDefault( "visible", "true" );
106         CheckDefault( "x", "0" );
107         CheckDefault( "y", "0" );
108         CheckDefault( "lefttop", "lefttop" );
109         CheckDefault( "rightbottom", "lefttop" );
110         CheckDefault( "down", "none" );
111         CheckDefault( "over", "none" );
112         CheckDefault( "action", "none" );
113         CheckDefault( "tooltiptext", "" );
114         CheckDefault( "help", "" );
115
116         const BuilderData::Button button( uniqueId( attr["id"] ),
117                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
118                 attr["lefttop"], attr["rightbottom"], attr["visible"],
119                 attr["up"], attr["down"], attr["over"], attr["action"],
120                 attr["tooltiptext"], attr["help"],
121                 m_curLayer, m_curWindowId, m_curLayoutId );
122         m_curLayer++;
123         m_data.m_listButton.push_back( button );
124     }
125
126     else if( rName == "Checkbox" )
127     {
128         RequireDefault( "up1" );
129         RequireDefault( "up2" );
130         RequireDefault( "state" );
131         CheckDefault( "id", "none" );
132         CheckDefault( "visible", "true" );
133         CheckDefault( "x", "0" );
134         CheckDefault( "y", "0" );
135         CheckDefault( "lefttop", "lefttop" );
136         CheckDefault( "rightbottom", "lefttop" );
137         CheckDefault( "down1", "none" );
138         CheckDefault( "over1", "none" );
139         CheckDefault( "down2", "none" );
140         CheckDefault( "over2", "none" );
141         CheckDefault( "action1", "none" );
142         CheckDefault( "action2", "none" );
143         CheckDefault( "tooltiptext1", "" );
144         CheckDefault( "tooltiptext2", "" );
145         CheckDefault( "help", "" );
146
147         const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ),
148                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
149                 attr["lefttop"], attr["rightbottom"], attr["visible"],
150                 attr["up1"], attr["down1"], attr["over1"],
151                 attr["up2"], attr["down2"], attr["over2"], attr["state"],
152                 attr["action1"], attr["action2"], attr["tooltiptext1"],
153                 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
154                 m_curLayoutId );
155         m_curLayer++;
156         m_data.m_listCheckbox.push_back( checkbox );
157     }
158
159     else if( rName == "Font" )
160     {
161         RequireDefault( "id" );
162         RequireDefault( "file" );
163         CheckDefault( "size", "12" );
164
165         const BuilderData::Font fontData( uniqueId( attr["id"] ),
166                 convertFileName( attr["file"] ),
167                 atoi( attr["size"] ) );
168         m_data.m_listFont.push_back( fontData );
169     }
170
171     else if( rName == "Group" )
172     {
173         CheckDefault( "x", "0" );
174         CheckDefault( "y", "0" );
175
176         m_xOffset += atoi( attr["x"] );
177         m_yOffset += atoi( attr["y"] );
178         m_xOffsetList.push_back( atoi( attr["x"] ) );
179         m_yOffsetList.push_back( atoi( attr["y"] ) );
180     }
181
182     else if( rName == "Image" )
183     {
184         RequireDefault( "image" );
185         CheckDefault( "id", "none" );
186         CheckDefault( "visible", "true" );
187         CheckDefault( "x", "0" );
188         CheckDefault( "y", "0" );
189         CheckDefault( "lefttop", "lefttop" );
190         CheckDefault( "rightbottom", "lefttop" );
191         CheckDefault( "action", "none" );
192         CheckDefault( "resize", "mosaic" );
193         CheckDefault( "help", "" );
194
195         const BuilderData::Image imageData( uniqueId( attr["id"] ),
196                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
197                 attr["lefttop"], attr["rightbottom"], attr["visible"],
198                 attr["image"], attr["action"], attr["resize"], attr["help"],
199                 m_curLayer, m_curWindowId, m_curLayoutId );
200         m_curLayer++;
201         m_data.m_listImage.push_back( imageData );
202     }
203
204     else if( rName == "Layout" )
205     {
206         RequireDefault( "width" );
207         RequireDefault( "height" );
208         CheckDefault( "id", "none" );
209         CheckDefault( "minwidth", "-1" );
210         CheckDefault( "maxwidth", "-1" );
211         CheckDefault( "minheight", "-1" );
212         CheckDefault( "maxheight", "-1" );
213
214         m_curLayoutId = uniqueId( attr["id"] );
215         const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
216                 atoi( attr["height"] ), atoi( attr["minwidth"] ),
217                 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
218                 atoi( attr["maxheight"] ), m_curWindowId );
219         m_data.m_listLayout.push_back( layout );
220         m_curLayer = 0;
221     }
222
223     else if( rName == "Playlist" )
224     {
225         RequireDefault( "id" );
226         RequireDefault( "font" );
227         CheckDefault( "visible", "true" );
228         CheckDefault( "x", "0" );
229         CheckDefault( "y", "0" );
230         CheckDefault( "width", "0" );
231         CheckDefault( "height", "0" );
232         CheckDefault( "lefttop", "lefttop" );
233         CheckDefault( "rightbottom", "lefttop" );
234         CheckDefault( "bgimage", "none" );
235         CheckDefault( "fgcolor", "#000000" );
236         CheckDefault( "playcolor", "#FF0000" );
237         CheckDefault( "bgcolor1", "#FFFFFF" );
238         CheckDefault( "bgcolor2", "#FFFFFF" );
239         CheckDefault( "selcolor", "#0000FF" );
240         CheckDefault( "help", "" );
241
242         m_curListId = uniqueId( attr["id"] );
243         const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
244                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
245                 atoi( attr["width"]), atoi( attr["height"] ),
246                 attr["lefttop"], attr["rightbottom"],
247                 attr["font"], "playlist", attr["bgimage"],
248                 convertColor( attr["fgcolor"] ),
249                 convertColor( attr["playcolor"] ),
250                 convertColor( attr["bgcolor1"] ),
251                 convertColor( attr["bgcolor2"] ),
252                 convertColor( attr["selcolor"] ), attr["help"],
253                 m_curLayer, m_curWindowId, m_curLayoutId );
254         m_curLayer++;
255         m_data.m_listList.push_back( listData );
256     }
257
258     else if( rName == "Playtree" )
259     {
260         RequireDefault( "id" );
261         RequireDefault( "font" );
262         CheckDefault( "visible", "true" );
263         CheckDefault( "x", "0" );
264         CheckDefault( "y", "0" );
265         CheckDefault( "width", "0" );
266         CheckDefault( "height", "0" );
267         CheckDefault( "lefttop", "lefttop" );
268         CheckDefault( "rightbottom", "lefttop" );
269         CheckDefault( "bgimage", "none" );
270         CheckDefault( "itemimage", "none" );
271         CheckDefault( "openimage", "none" );
272         CheckDefault( "closedimage", "none" );
273         CheckDefault( "fgcolor", "#000000" );
274         CheckDefault( "playcolor", "#FF0000" );
275         CheckDefault( "bgcolor1", "#FFFFFF" );
276         CheckDefault( "bgcolor2", "#FFFFFF" );
277         CheckDefault( "selcolor", "#0000FF" );
278         CheckDefault( "help", "" );
279
280         m_curTreeId = uniqueId( attr["id"] );
281         const BuilderData::Tree treeData( m_curTreeId, atoi( attr["x"] ) +
282                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
283                 atoi( attr["width"]), atoi( attr["height"] ),
284                 attr["lefttop"], attr["rightbottom"],
285                 attr["font"], "playtree",
286                 attr["bgimage"], attr["itemimage"],
287                 attr["openimage"], attr["closedimage"],
288                 convertColor( attr["fgcolor"] ),
289                 convertColor( attr["playcolor"] ),
290                 convertColor( attr["bgcolor1"] ),
291                 convertColor( attr["bgcolor2"] ),
292                 convertColor( attr["selcolor"] ), attr["help"],
293                 m_curLayer, m_curWindowId, m_curLayoutId );
294         m_curLayer++;
295         m_data.m_listTree.push_back( treeData );
296     }
297
298     else if( rName == "RadialSlider" )
299     {
300         RequireDefault( "sequence" );
301         RequireDefault( "nbimages" );
302         CheckDefault( "id", "none" );
303         CheckDefault( "visible", "true" );
304         CheckDefault( "x", "0" );
305         CheckDefault( "y", "0" );
306         CheckDefault( "lefttop", "lefttop" );
307         CheckDefault( "rightbottom", "lefttop" );
308         CheckDefault( "minangle", "0" );
309         CheckDefault( "maxangle", "360" );
310         CheckDefault( "value", "none" );
311         CheckDefault( "tooltiptext", "" );
312         CheckDefault( "help", "" );
313
314         const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
315                 attr["visible"],
316                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
317                 attr["lefttop"], attr["rightbottom"], attr["sequence"],
318                 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI /180,
319                 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
320                 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
321                 m_curLayoutId );
322         m_curLayer++;
323         m_data.m_listRadialSlider.push_back( radial );
324     }
325
326     else if( rName == "Slider" )
327     {
328         RequireDefault( "up" );
329         RequireDefault( "points" );
330         CheckDefault( "id", "none" );
331         CheckDefault( "visible", "true" );
332         CheckDefault( "x", "0" );
333         CheckDefault( "y", "0" );
334         CheckDefault( "width", "0" );
335         CheckDefault( "height", "0" );
336         CheckDefault( "lefttop", "lefttop" );
337         CheckDefault( "rightbottom", "lefttop" );
338         CheckDefault( "down", "none" );
339         CheckDefault( "over", "none" );
340         CheckDefault( "thickness", "10" );
341         CheckDefault( "value", "none" );
342         CheckDefault( "background", "none" );
343         CheckDefault( "nbimages", "1" );
344         CheckDefault( "tooltiptext", "" );
345         CheckDefault( "help", "" );
346
347         string newValue = attr["value"];
348         if( m_curListId != "" )
349         {
350             // Slider associated to a list
351             newValue = "playlist.slider";
352         }
353         else if( m_curTreeId != "" )
354         {
355             // Slider associated to a tree
356             newValue = "playtree.slider";
357         }
358         const BuilderData::Slider slider( uniqueId( attr["id"] ),
359                 attr["visible"],
360                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
361                 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
362                 attr["over"], attr["points"], atoi( attr["thickness"] ),
363                 newValue, attr["background"], atoi( attr["nbimages"] ),
364                 attr["tooltiptext"], attr["help"], m_curLayer,
365                 m_curWindowId, m_curLayoutId );
366         m_curLayer++;
367         m_data.m_listSlider.push_back( slider );
368     }
369
370     else if( rName == "Text" )
371     {
372         RequireDefault( "font" );
373         CheckDefault( "id", "none" );
374         CheckDefault( "visible", "true" );
375         CheckDefault( "x", "0" );
376         CheckDefault( "y", "0" );
377         CheckDefault( "text", "" );
378         CheckDefault( "color", "#000000" );
379         CheckDefault( "width", "0" );
380         CheckDefault( "lefttop", "lefttop" );
381         CheckDefault( "rightbottom", "lefttop" );
382         CheckDefault( "help", "" );
383
384         const BuilderData::Text textData( uniqueId( attr["id"] ),
385                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
386                 attr["visible"], attr["font"],
387                 attr["text"], atoi( attr["width"] ),
388                 attr["lefttop"], attr["rightbottom"],
389                 convertColor( attr["color"] ), attr["help"], m_curLayer,
390                 m_curWindowId, m_curLayoutId );
391         m_curLayer++;
392         m_data.m_listText.push_back( textData );
393     }
394
395     else if( rName == "Theme" )
396     {
397         RequireDefault( "version" );
398         CheckDefault( "tooltipfont", "defaultfont" );
399         CheckDefault( "magnet", "15" );
400         CheckDefault( "alpha", "255" );
401         CheckDefault( "movealpha", "255" );
402
403         // Check the version
404         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
405         {
406             msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
407                      attr["version"], SKINS_DTD_VERSION );
408             m_errors = true;
409             return;
410         }
411         const BuilderData::Theme theme( attr["tooltipfont"],
412                 atoi( attr["magnet"] ),
413                 convertInRange( attr["alpha"], 1, 255, "alpha" ),
414                 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
415         m_data.m_listTheme.push_back( theme );
416     }
417
418     else if( rName == "ThemeInfo" )
419     {
420         msg_Info( getIntf(), "skin: %s  author: %s", attr["name"],
421                   attr["author"] );
422     }
423
424     else if( rName == "Video" )
425     {
426         CheckDefault( "id", "none" );
427         CheckDefault( "visible", "true" );
428         CheckDefault( "x", "0" );
429         CheckDefault( "y", "0" );
430         CheckDefault( "width", "0" );
431         CheckDefault( "height", "0" );
432         CheckDefault( "lefttop", "lefttop" );
433         CheckDefault( "rightbottom", "lefttop" );
434         CheckDefault( "autoresize", "false" );
435         CheckDefault( "help", "" );
436
437         const BuilderData::Video videoData( uniqueId( attr["id"] ),
438                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
439                 atoi( attr["width"] ), atoi( attr["height" ]),
440                 attr["lefttop"], attr["rightbottom"],
441                 attr["visible"], convertBoolean( attr["autoresize"] ),
442                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
443         m_curLayer++;
444         m_data.m_listVideo.push_back( videoData );
445     }
446
447     else if( rName == "Window" )
448     {
449         CheckDefault( "id", "none" );
450         CheckDefault( "visible", "true" );
451         CheckDefault( "x", "0" );
452         CheckDefault( "y", "0" );
453         CheckDefault( "dragdrop", "true" );
454         CheckDefault( "playondrop", "true" );
455
456         m_curWindowId = uniqueId( attr["id"] );
457         const BuilderData::Window window( m_curWindowId,
458                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
459                 convertBoolean( attr["visible"] ),
460                 convertBoolean( attr["dragdrop"] ),
461                 convertBoolean( attr["playondrop"] ) );
462         m_data.m_listWindow.push_back( window );
463     }
464 }
465
466
467 void SkinParser::handleEndElement( const string &rName )
468 {
469     if( rName == "Group" )
470     {
471         m_xOffset -= m_xOffsetList.back();
472         m_yOffset -= m_yOffsetList.back();
473         m_xOffsetList.pop_back();
474         m_yOffsetList.pop_back();
475     }
476     else if( rName == "Playlist" )
477     {
478         m_curListId = "";
479     }
480     else if( rName == "Playtree" )
481     {
482         m_curTreeId = "";
483     }
484 }
485
486
487 bool SkinParser::convertBoolean( const char *value ) const
488 {
489     return strcmp( value, "true" ) == 0;
490 }
491
492
493 int SkinParser::convertColor( const char *transcolor ) const
494 {
495     unsigned long iRed, iGreen, iBlue;
496     iRed = iGreen = iBlue = 0;
497     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
498     return ( iRed << 16 | iGreen << 8 | iBlue );
499 }
500
501
502 string SkinParser::convertFileName( const char *fileName ) const
503 {
504     OSFactory *pFactory = OSFactory::instance( getIntf() );
505     return m_path + pFactory->getDirSeparator() + string( fileName );
506 }
507
508
509 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
510                                 const string &rAttribute ) const
511 {
512     int intValue = atoi( value );
513
514     if( intValue < minValue )
515     {
516         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
517                   "expected range [%i, %i], using %i instead",
518                   rAttribute.c_str(), intValue, minValue, maxValue, minValue );
519         return minValue;
520     }
521     else if( intValue > maxValue )
522     {
523         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
524                   "expected range [%i, %i], using %i instead",
525                   rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
526         return maxValue;
527     }
528     else
529     {
530         return intValue;
531     }
532 }
533
534
535 const string SkinParser::generateId() const
536 {
537     static int i = 1;
538
539     char genId[5];
540     snprintf( genId, 4, "%i", i++ );
541
542     string base = "_ReservedId_" + (string)genId;
543
544     return base;
545 }
546
547
548 const string SkinParser::uniqueId( const string &id )
549 {
550     string newId;
551
552     if( m_idSet.find( id ) != m_idSet.end() )
553     {
554         // The id was already used
555         if( id != "none" )
556         {
557             msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
558         }
559         newId = generateId();
560     }
561     else
562     {
563         // OK, this is a new id
564         newId = id;
565     }
566
567     // Add the id to the set
568     m_idSet.insert( newId );
569
570     return newId;
571 }