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