]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/skin_parser.cpp
* all: beginning of bitmap font support. At the moment only the digits
[vlc] / modules / gui / skins2 / parser / skin_parser.cpp
1 /*****************************************************************************
2  * skin_parser.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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 // Current DTD version
28 #define SKINS_DTD_VERSION "2.0"
29
30
31 SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName ):
32     XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 )
33 {
34 }
35
36
37 void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
38 {
39     if( rName == "Anchor" )
40     {
41         const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
42                 atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
43                 atoi( attr["priority"] ), attr["points"], m_curWindowId );
44         m_data.m_listAnchor.push_back( anchor );
45     }
46
47     else if( rName == "Bitmap" )
48     {
49         const BuilderData::Bitmap bitmap( attr["id"] , attr["file"],
50                 ConvertColor( attr["alphacolor"] ) );
51         m_data.m_listBitmap.push_back( bitmap );
52     }
53
54     else if( rName == "BitmapFont" )
55     {
56         const BuilderData::BitmapFont font( attr["id"] , attr["file"],
57                 attr["type"] );
58         m_data.m_listBitmapFont.push_back( font );
59     }
60
61     else if( rName == "Button" )
62     {
63         const BuilderData::Button button( uniqueId( attr["id"] ), atoi( attr["x"] ) +
64                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
65                 attr["rightbottom"], attr["visible"], attr["up"], attr["down"],
66                 attr["over"], attr["action"], attr["tooltiptext"], attr["help"],
67                 m_curLayer, m_curWindowId, m_curLayoutId );
68         m_curLayer++;
69         m_data.m_listButton.push_back( button );
70     }
71
72     else if( rName == "Checkbox" )
73     {
74         const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ), atoi( attr["x"] ) +
75                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
76                 attr["rightbottom"], attr["visible"], attr["up1"], attr["down1"], attr["over1"],
77                 attr["up2"], attr["down2"], attr["over2"], attr["state"],
78                 attr["action1"], attr["action2"], attr["tooltiptext1"],
79                 attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
80                 m_curLayoutId );
81         m_curLayer++;
82         m_data.m_listCheckbox.push_back( checkbox );
83     }
84
85     else if( rName == "Font" )
86     {
87         const BuilderData::Font fontData( attr["id"], attr["file"],
88                 atoi( attr["size"] ) );
89         m_data.m_listFont.push_back( fontData );
90     }
91
92     else if( rName == "Group" )
93     {
94         m_xOffset += atoi( attr["x"] );
95         m_yOffset += atoi( attr["y"] );
96         m_xOffsetList.push_back( atoi( attr["x"] ) );
97         m_yOffsetList.push_back( atoi( attr["y"] ) );
98     }
99
100     else if( rName == "Image" )
101     {
102         const BuilderData::Image imageData( uniqueId( attr["id"] ), atoi( attr["x"] ) +
103                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
104                 attr["rightbottom"], attr["visible"],
105                 attr["image"], attr["action"], attr["help"], m_curLayer,
106                 m_curWindowId, m_curLayoutId );
107         m_curLayer++;
108         m_data.m_listImage.push_back( imageData );
109     }
110
111     else if( rName == "Layout" )
112     {
113         m_curLayoutId = uniqueId( attr["id"] );
114         const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
115                 atoi( attr["height"] ), atoi( attr["minwidth"] ),
116                 atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
117                 atoi( attr["maxheight"] ), m_curWindowId );
118         m_data.m_listLayout.push_back( layout );
119         m_curLayer = 0;
120     }
121
122     else if( rName == "Playlist" )
123     {
124         m_curListId = uniqueId( attr["id"] );
125         const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
126                 m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
127                 atoi( attr["width"]), atoi( attr["height"] ),
128                 attr["lefttop"], attr["rightbottom"],
129                 attr["font"], attr["var"], ConvertColor( attr["fgcolor"] ),
130                 ConvertColor( attr["playcolor"] ),
131                 ConvertColor( attr["bgcolor1"] ),
132                 ConvertColor( attr["bgcolor2"] ),
133                 ConvertColor( attr["selcolor"] ), attr["help"],
134                 m_curLayer, m_curWindowId, m_curLayoutId );
135         m_curLayer++;
136         m_data.m_listList.push_back( listData );
137     }
138
139     else if( rName == "RadialSlider" )
140     {
141         const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
142                 attr["visible"],
143                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
144                 attr["lefttop"], attr["rightbottom"], attr["sequence"],
145                 atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI / 180,
146                 atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
147                 attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
148                 m_curLayoutId );
149         m_curLayer++;
150         m_data.m_listRadialSlider.push_back( radial );
151     }
152
153     else if( rName == "Slider" )
154     {
155         string newValue = attr["value"];
156         if( m_curListId != "" )
157         {
158             // Slider associated to a list
159             newValue = "playlist.slider";
160         }
161         const BuilderData::Slider slider( uniqueId( attr["id"] ),
162                 attr["visible"],
163                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
164                 attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
165                 attr["over"], attr["points"], atoi( attr["thickness"] ),
166                 newValue, attr["tooltiptext"], attr["help"], m_curLayer,
167                 m_curWindowId, m_curLayoutId );
168         m_curLayer++;
169         m_data.m_listSlider.push_back( slider );
170     }
171
172     else if( rName == "Text" )
173     {
174         const BuilderData::Text textData( uniqueId( attr["id"] ),
175                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
176                 attr["visible"], attr["font"],
177                 attr["text"], atoi( attr["width"] ),
178                 ConvertColor( attr["color"] ), attr["help"], m_curLayer,
179                 m_curWindowId, m_curLayoutId );
180         m_curLayer++;
181         m_data.m_listText.push_back( textData );
182     }
183
184     else if( rName == "Theme" )
185     {
186         // Check the version
187         if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
188         {
189             msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
190                      attr["version"], SKINS_DTD_VERSION );
191             m_errors = true;
192             return;
193         }
194         const BuilderData::Theme theme( attr["tooltipfont"],
195                 atoi( attr["magnet"] ), atoi( attr["alpha"] ),
196                 atoi( attr["movealpha"] ), atoi( attr["fadetime"] ) );
197         m_data.m_listTheme.push_back( theme );
198     }
199
200     else if( rName == "ThemeInfo" )
201     {
202         msg_Warn( getIntf(), "skin: %s  author: %s", attr["name"],
203                   attr["author"] );
204     }
205
206     else if( rName == "Video" )
207     {
208         const BuilderData::Video videoData( uniqueId( attr["id"] ),
209                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
210                 atoi( attr["width"] ), atoi( attr["height" ]),
211                 attr["lefttop"], attr["rightbottom"],
212                 attr["visible"], attr["help"], m_curLayer,
213                 m_curWindowId, m_curLayoutId );
214         m_curLayer++;
215         m_data.m_listVideo.push_back( videoData );
216     }
217
218     else if( rName == "Window" )
219     {
220         m_curWindowId = uniqueId( attr["id"] );
221         const BuilderData::Window window( m_curWindowId,
222                 atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
223                 ConvertBoolean( attr["visible"] ),
224                 ConvertBoolean( attr["dragdrop"] ),
225                 ConvertBoolean( attr["playondrop"] ) );
226         m_data.m_listWindow.push_back( window );
227     }
228 }
229
230
231 void SkinParser::handleEndElement( const string &rName )
232 {
233     if( rName == "Group" )
234     {
235         m_xOffset -= m_xOffsetList.back();
236         m_yOffset -= m_yOffsetList.back();
237         m_xOffsetList.pop_back();
238         m_yOffsetList.pop_back();
239     }
240
241     else if( rName == "Playlist" )
242     {
243         m_curListId = "";
244     }
245 }
246
247
248 bool SkinParser::ConvertBoolean( const char *value ) const
249 {
250     return strcmp( value, "true" ) == 0;
251 }
252
253
254 int SkinParser::ConvertColor( const char *transcolor ) const
255 {
256     unsigned long iRed, iGreen, iBlue;
257     iRed = iGreen = iBlue = 0;
258     sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
259     return ( iRed << 16 | iGreen << 8 | iBlue );
260 }
261
262 const string SkinParser::generateId() const
263 {
264     static int i = 1;
265
266     char genId[5];
267     snprintf( genId, 4, "%i", i++ );
268
269     string base = "_ReservedId_" + (string)genId;
270
271     return base;
272 }
273
274
275 const string SkinParser::uniqueId( const string &id )
276 {
277     string newId;
278
279     if( m_idSet.find( id ) != m_idSet.end() )
280     {
281         // The id was already used
282         if( id != "none" )
283         {
284             msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
285         }
286         newId = generateId();
287     }
288     else
289     {
290         // OK, this is a new id
291         newId = id;
292     }
293
294     // Add the id to the set
295     m_idSet.insert( newId );
296
297     return newId;
298 }
299