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