]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
* skin_parser.cpp: fixed the hack for nums_ex.bmp/numbers.bmp
[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( "tooltiptext", "" );
343         CheckDefault( "help", "" );
344
345         string newValue = attr["value"];
346         if( m_curListId != "" )
347         {
348             // Slider associated to a list
349             newValue = "playlist.slider";
350         }
351         else if( m_curTreeId != "" )
352         {
353             // Slider associated to a tree
354             newValue = "playtree.slider";
355         }
356         const BuilderData::Slider slider( uniqueId( attr["id"] ),
357                 attr["visible"],
358                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
359                 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
360                 attr["over"], attr["points"], atoi( attr["thickness"] ),
361                 newValue, attr["tooltiptext"], attr["help"], m_curLayer,
362                 m_curWindowId, m_curLayoutId );
363         m_curLayer++;
364         m_data.m_listSlider.push_back( slider );
365     }
366
367     else if( rName == "Text" )
368     {
369         RequireDefault( "font" );
370         CheckDefault( "id", "none" );
371         CheckDefault( "visible", "true" );
372         CheckDefault( "x", "0" );
373         CheckDefault( "y", "0" );
374         CheckDefault( "text", "" );
375         CheckDefault( "color", "#000000" );
376         CheckDefault( "width", "0" );
377         CheckDefault( "lefttop", "lefttop" );
378         CheckDefault( "rightbottom", "lefttop" );
379         CheckDefault( "help", "" );
380
381         const BuilderData::Text textData( uniqueId( attr["id"] ),
382                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
383                 attr["visible"], attr["font"],
384                 attr["text"], atoi( attr["width"] ),
385                 attr["lefttop"], attr["rightbottom"],
386                 convertColor( attr["color"] ), attr["help"], m_curLayer,
387                 m_curWindowId, m_curLayoutId );
388         m_curLayer++;
389         m_data.m_listText.push_back( textData );
390     }
391
392     else if( rName == "Theme" )
393     {
394         RequireDefault( "version" );
395         CheckDefault( "tooltipfont", "defaultfont" );
396         CheckDefault( "magnet", "15" );
397         CheckDefault( "alpha", "255" );
398         CheckDefault( "movealpha", "255" );
399
400         // Check the version
401         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
402         {
403             msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
404                      attr["version"], SKINS_DTD_VERSION );
405             m_errors = true;
406             return;
407         }
408         const BuilderData::Theme theme( attr["tooltipfont"],
409                 atoi( attr["magnet"] ),
410                 convertInRange( attr["alpha"], 1, 255, "alpha" ),
411                 convertInRange( attr["movealpha"], 1, 255, "movealpha" ) );
412         m_data.m_listTheme.push_back( theme );
413     }
414
415     else if( rName == "ThemeInfo" )
416     {
417         msg_Info( getIntf(), "skin: %s  author: %s", attr["name"],
418                   attr["author"] );
419     }
420
421     else if( rName == "Video" )
422     {
423         CheckDefault( "id", "none" );
424         CheckDefault( "visible", "true" );
425         CheckDefault( "x", "0" );
426         CheckDefault( "y", "0" );
427         CheckDefault( "width", "0" );
428         CheckDefault( "height", "0" );
429         CheckDefault( "lefttop", "lefttop" );
430         CheckDefault( "rightbottom", "lefttop" );
431         CheckDefault( "autoresize", "false" );
432         CheckDefault( "help", "" );
433
434         const BuilderData::Video videoData( uniqueId( attr["id"] ),
435                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
436                 atoi( attr["width"] ), atoi( attr["height" ]),
437                 attr["lefttop"], attr["rightbottom"],
438                 attr["visible"], convertBoolean( attr["autoresize"] ),
439                 attr["help"], m_curLayer, m_curWindowId, m_curLayoutId );
440         m_curLayer++;
441         m_data.m_listVideo.push_back( videoData );
442     }
443
444     else if( rName == "Window" )
445     {
446         CheckDefault( "id", "none" );
447         CheckDefault( "visible", "true" );
448         CheckDefault( "x", "0" );
449         CheckDefault( "y", "0" );
450         CheckDefault( "dragdrop", "true" );
451         CheckDefault( "playondrop", "true" );
452
453         m_curWindowId = uniqueId( attr["id"] );
454         const BuilderData::Window window( m_curWindowId,
455                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
456                 convertBoolean( attr["visible"] ),
457                 convertBoolean( attr["dragdrop"] ),
458                 convertBoolean( attr["playondrop"] ) );
459         m_data.m_listWindow.push_back( window );
460     }
461 }
462
463
464 void SkinParser::handleEndElement( const string &rName )
465 {
466     if( rName == "Group" )
467     {
468         m_xOffset -= m_xOffsetList.back();
469         m_yOffset -= m_yOffsetList.back();
470         m_xOffsetList.pop_back();
471         m_yOffsetList.pop_back();
472     }
473     else if( rName == "Playlist" )
474     {
475         m_curListId = "";
476     }
477     else if( rName == "Playtree" )
478     {
479         m_curTreeId = "";
480     }
481 }
482
483
484 bool SkinParser::convertBoolean( const char *value ) const
485 {
486     return strcmp( value, "true" ) == 0;
487 }
488
489
490 int SkinParser::convertColor( const char *transcolor ) const
491 {
492     unsigned long iRed, iGreen, iBlue;
493     iRed = iGreen = iBlue = 0;
494     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
495     return ( iRed << 16 | iGreen << 8 | iBlue );
496 }
497
498
499 string SkinParser::convertFileName( const char *fileName ) const
500 {
501     OSFactory *pFactory = OSFactory::instance( getIntf() );
502     return m_path + pFactory->getDirSeparator() + string( fileName );
503 }
504
505
506 int SkinParser::convertInRange( const char *value, int minValue, int maxValue,
507                                 const string &rAttribute ) const
508 {
509     int intValue = atoi( value );
510
511     if( intValue < minValue )
512     {
513         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
514                   "expected range [%i, %i], using %i instead",
515                   rAttribute.c_str(), intValue, minValue, maxValue, minValue );
516         return minValue;
517     }
518     else if( intValue > maxValue )
519     {
520         msg_Warn( getIntf(), "Value of \"%s\" attribute (%i) is out of the "
521                   "expected range [%i, %i], using %i instead",
522                   rAttribute.c_str(), intValue, minValue, maxValue, maxValue );
523         return maxValue;
524     }
525     else
526     {
527         return intValue;
528     }
529 }
530
531
532 const string SkinParser::generateId() const
533 {
534     static int i = 1;
535
536     char genId[5];
537     snprintf( genId, 4, "%i", i++ );
538
539     string base = "_ReservedId_" + (string)genId;
540
541     return base;
542 }
543
544
545 const string SkinParser::uniqueId( const string &id )
546 {
547     string newId;
548
549     if( m_idSet.find( id ) != m_idSet.end() )
550     {
551         // The id was already used
552         if( id != "none" )
553         {
554             msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
555         }
556         newId = generateId();
557     }
558     else
559     {
560         // OK, this is a new id
561         newId = id;
562     }
563
564     // Add the id to the set
565     m_idSet.insert( newId );
566
567     return newId;
568 }