]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/builder.cpp
9eba9dc3631a66102dd926ea5bf4f76088599163
[vlc] / modules / gui / skins2 / parser / builder.cpp
1 /*****************************************************************************
2  * builder.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <string.h>
26 #include "builder.hpp"
27 #include "builder_data.hpp"
28 #include "interpreter.hpp"
29 #include "skin_parser.hpp"
30 #include "../src/file_bitmap.hpp"
31 #include "../src/os_factory.hpp"
32 #include "../src/generic_bitmap.hpp"
33 #include "../src/top_window.hpp"
34 #include "../src/anchor.hpp"
35 #include "../src/bitmap_font.hpp"
36 #include "../src/ft2_font.hpp"
37 #include "../src/ini_file.hpp"
38 #include "../src/generic_layout.hpp"
39 #include "../src/popup.hpp"
40 #include "../src/theme.hpp"
41 #include "../src/window_manager.hpp"
42 #include "../commands/cmd_generic.hpp"
43 #include "../controls/ctrl_button.hpp"
44 #include "../controls/ctrl_checkbox.hpp"
45 #include "../controls/ctrl_image.hpp"
46 #include "../controls/ctrl_list.hpp"
47 #include "../controls/ctrl_move.hpp"
48 #include "../controls/ctrl_resize.hpp"
49 #include "../controls/ctrl_slider.hpp"
50 #include "../controls/ctrl_radialslider.hpp"
51 #include "../controls/ctrl_text.hpp"
52 #include "../controls/ctrl_tree.hpp"
53 #include "../controls/ctrl_video.hpp"
54 #include "../utils/bezier.hpp"
55 #include "../utils/position.hpp"
56 #include "../utils/var_bool.hpp"
57 #include "../utils/var_text.hpp"
58
59 #include "vlc_image.h"
60
61
62 Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData,
63                   const string &rPath ):
64     SkinObject( pIntf ), m_rData( rData ), m_path( rPath ), m_pTheme( NULL )
65 {
66     m_pImageHandler = image_HandlerCreate( pIntf );
67 }
68
69 Builder::~Builder()
70 {
71     if( m_pImageHandler ) image_HandlerDelete( m_pImageHandler );
72 }
73
74 CmdGeneric *Builder::parseAction( const string &rAction )
75 {
76     return Interpreter::instance( getIntf() )->parseAction( rAction, m_pTheme );
77 }
78
79
80 // Useful macro
81 #define ADD_OBJECTS( type ) \
82     list<BuilderData::type>::const_iterator it##type; \
83     for( it##type = m_rData.m_list##type.begin(); \
84          it##type != m_rData.m_list##type.end(); it##type++ ) \
85     { \
86         add##type( *it##type ); \
87     }
88
89
90 Theme *Builder::build()
91 {
92     m_pTheme = new Theme( getIntf() );
93     if( m_pTheme == NULL )
94     {
95         return NULL;
96     }
97
98     // Create everything from the data in the XML
99     ADD_OBJECTS( Theme );
100     ADD_OBJECTS( IniFile );
101     ADD_OBJECTS( Bitmap );
102     ADD_OBJECTS( SubBitmap );
103     ADD_OBJECTS( BitmapFont );
104     ADD_OBJECTS( Font );
105     ADD_OBJECTS( Window );
106     // XXX: PopupMenus are created after the windows, so that the Win32Factory
107     // (at least) can give a valid window handle to the OSPopup objects
108     ADD_OBJECTS( PopupMenu );
109     ADD_OBJECTS( Layout );
110     ADD_OBJECTS( Anchor );
111     ADD_OBJECTS( Button );
112     ADD_OBJECTS( Checkbox );
113     ADD_OBJECTS( Image );
114     ADD_OBJECTS( Text );
115     ADD_OBJECTS( RadialSlider );
116     ADD_OBJECTS( Slider );
117     ADD_OBJECTS( List );
118     ADD_OBJECTS( Tree );
119     ADD_OBJECTS( Video );
120     // MenuItems must be created after all the rest, so that the IDs of the
121     // other elements exist and can be parsed in the actions
122     ADD_OBJECTS( MenuItem );
123     ADD_OBJECTS( MenuSeparator );
124
125     return m_pTheme;
126 }
127
128
129 // Macro to get a bitmap by its ID in the builder
130 #define GET_BMP( pBmp, id ) \
131     if( id != "none" ) \
132     { \
133         pBmp = m_pTheme->getBitmapById(id); \
134         if( pBmp == NULL ) \
135         { \
136             msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
137             return; \
138         } \
139     }
140
141 void Builder::addTheme( const BuilderData::Theme &rData )
142 {
143     WindowManager &rManager = m_pTheme->getWindowManager();
144     rManager.setMagnetValue( rData.m_magnet );
145     rManager.setAlphaValue( rData.m_alpha );
146     rManager.setMoveAlphaValue( rData.m_moveAlpha );
147     GenericFont *pFont = getFont( rData.m_tooltipfont );
148     if( pFont )
149     {
150         rManager.createTooltip( *pFont );
151     }
152     else
153     {
154         msg_Warn( getIntf(), "invalid tooltip font: %s",
155                   rData.m_tooltipfont.c_str() );
156     }
157 }
158
159
160 void Builder::addIniFile( const BuilderData::IniFile &rData )
161 {
162     // Parse the INI file
163     IniFile iniFile( getIntf(), rData.m_id, getFilePath( rData.m_file ) );
164     iniFile.parseFile();
165 }
166
167
168 void Builder::addBitmap( const BuilderData::Bitmap &rData )
169 {
170     GenericBitmap *pBmp =
171         new FileBitmap( getIntf(), m_pImageHandler,
172                         getFilePath( rData.m_fileName ), rData.m_alphaColor,
173                         rData.m_nbFrames, rData.m_fps );
174     if( !pBmp->getData() )
175     {
176         // Invalid bitmap
177         delete pBmp;
178         return;
179     }
180     m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
181 }
182
183
184 void Builder::addSubBitmap( const BuilderData::SubBitmap &rData )
185 {
186     if( m_pTheme->m_bitmaps.find( rData.m_id ) != m_pTheme->m_bitmaps.end() )
187     {
188         msg_Dbg( getIntf(), "bitmap %s already exists", rData.m_id.c_str() );
189         return;
190     }
191
192     // Get the parent bitmap
193     GenericBitmap *pParentBmp = NULL;
194     GET_BMP( pParentBmp, rData.m_parent );
195
196     // Copy a region of the parent bitmap to the new one
197     BitmapImpl *pBmp =
198         new BitmapImpl( getIntf(), rData.m_width, rData.m_height,
199                         rData.m_nbFrames, rData.m_fps );
200     bool res = pBmp->drawBitmap( *pParentBmp, rData.m_x, rData.m_y, 0, 0,
201                                  rData.m_width, rData.m_height );
202     if( !res )
203     {
204         // Invalid sub-bitmap
205         delete pBmp;
206         msg_Warn( getIntf(), "sub-bitmap %s ignored", rData.m_id.c_str() );
207         return;
208     }
209     m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
210 }
211
212
213 void Builder::addBitmapFont( const BuilderData::BitmapFont &rData )
214 {
215     if( m_pTheme->m_fonts.find( rData.m_id ) != m_pTheme->m_fonts.end() )
216     {
217         msg_Dbg( getIntf(), "font %s already exists", rData.m_id.c_str() );
218         return;
219     }
220
221     GenericBitmap *pBmp =
222         new FileBitmap( getIntf(), m_pImageHandler,
223                         getFilePath( rData.m_file ), 0 );
224     if( !pBmp->getData() )
225     {
226         // Invalid bitmap
227         delete pBmp;
228         return;
229     }
230
231     m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
232
233     GenericFont *pFont = new BitmapFont( getIntf(), *pBmp, rData.m_type );
234     if( pFont->init() )
235     {
236         m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
237     }
238     else
239     {
240         delete pFont;
241     }
242 }
243
244
245 void Builder::addFont( const BuilderData::Font &rData )
246 {
247     // Try to load the font from the theme directory
248     GenericFont *pFont = new FT2Font( getIntf(),
249                                       getFilePath( rData.m_fontFile ),
250                                       rData.m_size );
251     if( pFont->init() )
252     {
253         m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
254     }
255     else
256     {
257         delete pFont;
258
259         // Font not found; try in the resource path
260         OSFactory *pOSFactory = OSFactory::instance( getIntf() );
261         const list<string> &resPath = pOSFactory->getResourcePath();
262         const string &sep = pOSFactory->getDirSeparator();
263
264         list<string>::const_iterator it;
265         for( it = resPath.begin(); it != resPath.end(); it++ )
266         {
267             string path = (*it) + sep + "fonts" + sep + rData.m_fontFile;
268             pFont = new FT2Font( getIntf(), path, rData.m_size );
269             if( pFont->init() )
270             {
271                 // Font loaded successfully
272                 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
273                 break;
274             }
275             else
276             {
277                 delete pFont;
278             }
279         }
280     }
281 }
282
283
284 void Builder::addPopupMenu( const BuilderData::PopupMenu &rData )
285 {
286     Popup *pPopup = new Popup( getIntf(), m_pTheme->getWindowManager() );
287
288     m_pTheme->m_popups[rData.m_id] = PopupPtr( pPopup );
289 }
290
291
292 void Builder::addMenuItem( const BuilderData::MenuItem &rData )
293 {
294     Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
295     if( pPopup == NULL )
296     {
297         msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
298         return;
299     }
300
301     CmdGeneric *pCommand = parseAction( rData.m_action );
302     if( pCommand == NULL )
303     {
304         msg_Err( getIntf(), "invalid action: %s", rData.m_action.c_str() );
305         return;
306     }
307
308     pPopup->addItem( rData.m_label, *pCommand, rData.m_pos );
309 }
310
311
312 void Builder::addMenuSeparator( const BuilderData::MenuSeparator &rData )
313 {
314     Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
315     if( pPopup == NULL )
316     {
317         msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
318         return;
319     }
320
321     pPopup->addSeparator( rData.m_pos );
322 }
323
324
325 void Builder::addWindow( const BuilderData::Window &rData )
326 {
327     TopWindow *pWin =
328         new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
329                        m_pTheme->getWindowManager(),
330                        rData.m_dragDrop, rData.m_playOnDrop,
331                        rData.m_visible );
332
333     m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
334 }
335
336
337 void Builder::addLayout( const BuilderData::Layout &rData )
338 {
339     TopWindow *pWin = m_pTheme->getWindowById( rData.m_windowId );
340     if( pWin == NULL )
341     {
342         msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
343         return;
344     }
345
346     int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
347     int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
348     int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
349                     rData.m_height;
350     int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
351                     rData.m_height;
352     GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
353                                                 rData.m_height,
354                                                 minWidth, maxWidth, minHeight,
355                                                 maxHeight );
356     m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
357
358     // Attach the layout to its window
359     m_pTheme->getWindowManager().addLayout( *pWin, *pLayout );
360 }
361
362
363 void Builder::addAnchor( const BuilderData::Anchor &rData )
364 {
365     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
366     if( pLayout == NULL )
367     {
368         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
369         return;
370     }
371
372     Bezier *pCurve = getPoints( rData.m_points.c_str() );
373     if( pCurve == NULL )
374     {
375         msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
376                  rData.m_points.c_str() );
377         return;
378     }
379     m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
380
381     // Compute the position of the anchor
382     const Position pos = makePosition( rData.m_leftTop, rData.m_leftTop,
383                                        rData.m_xPos, rData.m_yPos,
384                                        pCurve->getWidth(),
385                                        pCurve->getHeight(), *pLayout );
386
387     Anchor *pAnc = new Anchor( getIntf(), pos, rData.m_range, rData.m_priority,
388                                *pCurve, *pLayout );
389     pLayout->addAnchor( pAnc );
390 }
391
392
393 void Builder::addButton( const BuilderData::Button &rData )
394 {
395     // Get the bitmaps of the button
396     GenericBitmap *pBmpUp = NULL;
397     GET_BMP( pBmpUp, rData.m_upId );
398
399     GenericBitmap *pBmpDown = pBmpUp;
400     GET_BMP( pBmpDown, rData.m_downId );
401
402     GenericBitmap *pBmpOver = pBmpUp;
403     GET_BMP( pBmpOver, rData.m_overId );
404
405     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
406     if( pLayout == NULL )
407     {
408         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
409         return;
410     }
411
412     CmdGeneric *pCommand = parseAction( rData.m_actionId );
413     if( pCommand == NULL )
414     {
415         msg_Err( getIntf(), "invalid action: %s", rData.m_actionId.c_str() );
416         return;
417     }
418
419     // Get the visibility variable
420     // XXX check when it is null
421     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
422     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
423
424     CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
425         *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
426         UString( getIntf(), rData.m_help.c_str() ), pVisible );
427
428     // Compute the position of the control
429     // XXX (we suppose all the images have the same size...)
430     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
431                                        rData.m_xPos, rData.m_yPos,
432                                        pBmpUp->getWidth(),
433                                        pBmpUp->getHeight(), *pLayout );
434
435     pLayout->addControl( pButton, pos, rData.m_layer );
436
437     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
438 }
439
440
441 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
442 {
443     // Get the bitmaps of the checkbox
444     GenericBitmap *pBmpUp1 = NULL;
445     GET_BMP( pBmpUp1, rData.m_up1Id );
446
447     GenericBitmap *pBmpDown1 = pBmpUp1;
448     GET_BMP( pBmpDown1, rData.m_down1Id );
449
450     GenericBitmap *pBmpOver1 = pBmpUp1;
451     GET_BMP( pBmpOver1, rData.m_over1Id );
452
453     GenericBitmap *pBmpUp2 = NULL;
454     GET_BMP( pBmpUp2, rData.m_up2Id );
455
456     GenericBitmap *pBmpDown2 = pBmpUp2;
457     GET_BMP( pBmpDown2, rData.m_down2Id );
458
459     GenericBitmap *pBmpOver2 = pBmpUp2;
460     GET_BMP( pBmpOver2, rData.m_over2Id );
461
462     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
463     if( pLayout == NULL )
464     {
465         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
466         return;
467     }
468
469     CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
470     if( pCommand1 == NULL )
471     {
472         msg_Err( getIntf(), "invalid action: %s", rData.m_action1.c_str() );
473         return;
474     }
475
476     CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
477     if( pCommand2 == NULL )
478     {
479         msg_Err( getIntf(), "invalid action: %s", rData.m_action2.c_str() );
480         return;
481     }
482
483     // Get the state variable
484     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
485     VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
486     if( pVar == NULL )
487     {
488         // TODO: default state
489         return;
490     }
491
492     // Get the visibility variable
493     // XXX check when it is null
494     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
495
496     // Create the control
497     CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
498         *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
499         *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
500         UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
501         UString( getIntf(), rData.m_help.c_str() ), pVisible );
502
503     // Compute the position of the control
504     // XXX (we suppose all the images have the same size...)
505     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
506                                        rData.m_xPos, rData.m_yPos,
507                                        pBmpUp1->getWidth(),
508                                        pBmpUp1->getHeight(), *pLayout );
509
510     pLayout->addControl( pCheckbox, pos, rData.m_layer );
511
512     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
513 }
514
515
516 void Builder::addImage( const BuilderData::Image &rData )
517 {
518     GenericBitmap *pBmp = NULL;
519     GET_BMP( pBmp, rData.m_bmpId );
520
521     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
522     if( pLayout == NULL )
523     {
524         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
525         return;
526     }
527
528     TopWindow *pWindow = m_pTheme->getWindowById( rData.m_windowId );
529     if( pWindow == NULL )
530     {
531         msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
532         return;
533     }
534
535     CmdGeneric *pCommand = parseAction( rData.m_action2Id );
536     if( pCommand == NULL )
537     {
538         msg_Err( getIntf(), "invalid action: %s", rData.m_action2Id.c_str() );
539         return;
540     }
541
542     // Get the visibility variable
543     // XXX check when it is null
544     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
545     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
546
547     CtrlImage::resize_t resizeMethod =
548         (rData.m_resize == "scale" ? CtrlImage::kScale : CtrlImage::kMosaic);
549     CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, *pCommand,
550         resizeMethod, UString( getIntf(), rData.m_help.c_str() ), pVisible );
551
552     // Compute the position of the control
553     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
554                                        rData.m_xPos,
555                                        rData.m_yPos, pBmp->getWidth(),
556                                        pBmp->getHeight(), *pLayout );
557
558     // XXX: test to be changed! XXX
559     if( rData.m_actionId == "move" )
560     {
561         CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
562              *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
563              pVisible );
564         pLayout->addControl( pMove, pos, rData.m_layer );
565     }
566     else if( rData.m_actionId == "resizeS" )
567     {
568         CtrlResize *pResize = new CtrlResize( getIntf(),
569                 m_pTheme->getWindowManager(), *pImage, *pLayout,
570                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
571                 WindowManager::kResizeS );
572         pLayout->addControl( pResize, pos, rData.m_layer );
573     }
574     else if( rData.m_actionId == "resizeE" )
575     {
576         CtrlResize *pResize = new CtrlResize( getIntf(),
577                 m_pTheme->getWindowManager(), *pImage, *pLayout,
578                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
579                 WindowManager::kResizeE );
580         pLayout->addControl( pResize, pos, rData.m_layer );
581     }
582     else if( rData.m_actionId == "resizeSE" )
583     {
584         CtrlResize *pResize = new CtrlResize( getIntf(),
585                 m_pTheme->getWindowManager(), *pImage, *pLayout,
586                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
587                 WindowManager::kResizeSE );
588         pLayout->addControl( pResize, pos, rData.m_layer );
589     }
590     else
591     {
592         pLayout->addControl( pImage, pos, rData.m_layer );
593     }
594
595     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
596 }
597
598
599 void Builder::addText( const BuilderData::Text &rData )
600 {
601     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
602     if( pLayout == NULL )
603     {
604         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
605         return;
606     }
607
608     GenericFont *pFont = getFont( rData.m_fontId );
609     if( pFont == NULL )
610     {
611         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
612         return;
613     }
614
615     // Convert the scrolling mode
616     CtrlText::Scrolling_t scrolling;
617     if( rData.m_scrolling == "auto" )
618         scrolling = CtrlText::kAutomatic;
619     else if( rData.m_scrolling == "manual" )
620         scrolling = CtrlText::kManual;
621     else if( rData.m_scrolling == "none" )
622         scrolling = CtrlText::kNone;
623     else
624     {
625         msg_Err( getIntf(), "invalid scrolling mode: %s",
626                  rData.m_scrolling.c_str() );
627         return;
628     }
629
630     // Convert the alignment
631     CtrlText::Align_t alignment;
632     if( rData.m_alignment == "left" )
633         alignment = CtrlText::kLeft;
634     else if( rData.m_alignment == "center" || rData.m_alignment == "centre" )
635         alignment = CtrlText::kCenter;
636     else if( rData.m_alignment == "right" )
637         alignment = CtrlText::kRight;
638     else
639     {
640         msg_Err( getIntf(), "invalid alignment: %s",
641                  rData.m_alignment.c_str() );
642         return;
643     }
644
645     // Create a text variable
646     VarText *pVar = new VarText( getIntf() );
647     m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
648
649     // Get the visibility variable
650     // XXX check when it is null
651     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
652     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
653
654     CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
655         UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible,
656         scrolling, alignment );
657
658     int height = pFont->getSize();
659
660     // Compute the position of the control
661     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
662                                        rData.m_xPos, rData.m_yPos,
663                                        rData.m_width, height,
664                                        *pLayout );
665
666     pLayout->addControl( pText, pos, rData.m_layer );
667
668     // Set the text of the control
669     UString msg( getIntf(), rData.m_text.c_str() );
670     pVar->set( msg );
671
672     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
673 }
674
675
676 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
677 {
678     // Get the bitmaps of the slider
679     GenericBitmap *pSeq = NULL;
680     GET_BMP( pSeq, rData.m_sequence );
681
682     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
683     if( pLayout == NULL )
684     {
685         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
686         return;
687     }
688
689     // Get the variable associated to the slider
690     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
691     VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
692     if( pVar == NULL )
693     {
694         msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
695         return;
696     }
697
698     // Get the visibility variable
699     // XXX check when it is null
700     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
701
702     // Create the control
703     CtrlRadialSlider *pRadial =
704         new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
705                               rData.m_minAngle, rData.m_maxAngle,
706                               UString( getIntf(), rData.m_help.c_str() ),
707                               pVisible );
708
709     // XXX: resizing is not supported
710     // Compute the position of the control
711     const Position pos =
712         makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
713                       rData.m_yPos, pSeq->getWidth(),
714                       pSeq->getHeight() / rData.m_nbImages, *pLayout );
715
716     pLayout->addControl( pRadial, pos, rData.m_layer );
717
718     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
719 }
720
721
722 void Builder::addSlider( const BuilderData::Slider &rData )
723 {
724     // Add the background first, so that we will still have something almost
725     // functional if the cursor cannot be created properly (this happens for
726     // some winamp2 skins, where the images of the cursor are not always
727     // present)
728
729     // Get the bitmaps of the background
730     GenericBitmap *pBgImage = NULL;
731     if( rData.m_imageId != "none" )
732     {
733         GET_BMP( pBgImage, rData.m_imageId );
734     }
735
736     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
737     if( pLayout == NULL )
738     {
739         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
740         return;
741     }
742
743     Bezier *pCurve = getPoints( rData.m_points.c_str() );
744     if( pCurve == NULL )
745     {
746         msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
747                  rData.m_points.c_str() );
748         return;
749     }
750     m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
751
752     // Get the visibility variable
753     // XXX check when it is null
754     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
755     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
756
757     // Get the variable associated to the slider
758     VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
759     if( pVar == NULL )
760     {
761         msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
762         return;
763     }
764
765     // Create the background control
766     CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(),
767         *pCurve, *pVar, rData.m_thickness, pBgImage, rData.m_nbHoriz,
768         rData.m_nbVert, rData.m_padHoriz, rData.m_padVert,
769         pVisible, UString( getIntf(), rData.m_help.c_str() ) );
770
771     // Compute the position of the control
772     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
773                                        rData.m_xPos, rData.m_yPos,
774                                        pCurve->getWidth(), pCurve->getHeight(),
775                                        *pLayout );
776
777     pLayout->addControl( pBackground, pos, rData.m_layer );
778
779     m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
780
781     // Get the bitmaps of the cursor
782     GenericBitmap *pBmpUp = NULL;
783     GET_BMP( pBmpUp, rData.m_upId );
784
785     GenericBitmap *pBmpDown = pBmpUp;
786     GET_BMP( pBmpDown, rData.m_downId );
787
788     GenericBitmap *pBmpOver = pBmpUp;
789     GET_BMP( pBmpOver, rData.m_overId );
790
791     // Create the cursor control
792     CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
793         *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
794         UString( getIntf(), rData.m_tooltip.c_str() ),
795         UString( getIntf(), rData.m_help.c_str() ) );
796
797     pLayout->addControl( pCursor, pos, rData.m_layer );
798
799     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
800
801     // Associate the cursor to the background
802     pBackground->associateCursor( *pCursor );
803 }
804
805
806 void Builder::addList( const BuilderData::List &rData )
807 {
808     // Get the background bitmap, if any
809     GenericBitmap *pBgBmp = NULL;
810     GET_BMP( pBgBmp, rData.m_bgImageId );
811
812     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
813     if( pLayout == NULL )
814     {
815         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
816         return;
817     }
818
819     GenericFont *pFont = getFont( rData.m_fontId );
820     if( pFont == NULL )
821     {
822         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
823         return;
824     }
825
826     // Get the list variable
827     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
828     VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
829     if( pVar == NULL )
830     {
831         msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
832         return;
833     }
834
835     // Get the visibility variable
836     // XXX check when it is null
837     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
838
839     // Get the color values
840     uint32_t fgColor = getColor( rData.m_fgColor );
841     uint32_t playColor = getColor( rData.m_playColor );
842     uint32_t bgColor1 = getColor( rData.m_bgColor1 );
843     uint32_t bgColor2 = getColor( rData.m_bgColor2 );
844     uint32_t selColor = getColor( rData.m_selColor );
845
846     // Create the list control
847     CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
848        fgColor, playColor, bgColor1, bgColor2, selColor,
849        UString( getIntf(), rData.m_help.c_str() ), pVisible );
850
851     // Compute the position of the control
852     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
853                                        rData.m_xPos, rData.m_yPos,
854                                        rData.m_width, rData.m_height,
855                                        *pLayout );
856
857     pLayout->addControl( pList, pos, rData.m_layer );
858
859     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
860 }
861
862 void Builder::addTree( const BuilderData::Tree &rData )
863 {
864     // Get the bitmaps, if any
865     GenericBitmap *pBgBmp = NULL;
866     GenericBitmap *pItemBmp = NULL;
867     GenericBitmap *pOpenBmp = NULL;
868     GenericBitmap *pClosedBmp = NULL;
869     GET_BMP( pBgBmp, rData.m_bgImageId );
870     GET_BMP( pItemBmp, rData.m_itemImageId );
871     GET_BMP( pOpenBmp, rData.m_openImageId );
872     GET_BMP( pClosedBmp, rData.m_closedImageId );
873
874     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
875     if( pLayout == NULL )
876     {
877         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
878         return;
879     }
880
881     GenericFont *pFont = getFont( rData.m_fontId );
882     if( pFont == NULL )
883     {
884         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
885         return;
886     }
887
888     // Get the list variable
889     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
890     VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
891     if( pVar == NULL )
892     {
893         msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
894         return;
895     }
896
897     // Get the visibility variable
898     // XXX check when it is null
899     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
900     VarBool *pFlat = pInterpreter->getVarBool( rData.m_flat, m_pTheme );
901
902     // Get the color values
903     uint32_t fgColor = getColor( rData.m_fgColor );
904     uint32_t playColor = getColor( rData.m_playColor );
905     uint32_t bgColor1 = getColor( rData.m_bgColor1 );
906     uint32_t bgColor2 = getColor( rData.m_bgColor2 );
907     uint32_t selColor = getColor( rData.m_selColor );
908
909     // Create the list control
910     CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
911        pItemBmp, pOpenBmp, pClosedBmp,
912        fgColor, playColor, bgColor1, bgColor2, selColor,
913        UString( getIntf(), rData.m_help.c_str() ), pVisible, pFlat );
914
915     // Compute the position of the control
916     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
917                                        rData.m_xPos, rData.m_yPos,
918                                        rData.m_width, rData.m_height,
919                                        *pLayout );
920
921     pLayout->addControl( pTree, pos, rData.m_layer );
922
923     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
924 }
925
926 void Builder::addVideo( const BuilderData::Video &rData )
927 {
928     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
929     if( pLayout == NULL )
930     {
931         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
932         return;
933     }
934
935     // Get the visibility variable
936     // XXX check when it is null
937     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
938     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
939
940     CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
941         rData.m_autoResize, UString( getIntf(), rData.m_help.c_str() ),
942         pVisible );
943
944     // Compute the position of the control
945     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
946                                        rData.m_xPos, rData.m_yPos,
947                                        rData.m_width, rData.m_height,
948                                        *pLayout );
949
950     pLayout->addControl( pVideo, pos, rData.m_layer );
951
952     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
953 }
954
955
956 const Position Builder::makePosition( const string &rLeftTop,
957                                       const string &rRightBottom,
958                                       int xPos, int yPos, int width,
959                                       int height, const Box &rBox ) const
960 {
961     int left = 0, top = 0, right = 0, bottom = 0;
962     Position::Ref_t refLeftTop = Position::kLeftTop;
963     Position::Ref_t refRightBottom = Position::kLeftTop;
964
965     int boxWidth = rBox.getWidth();
966     int boxHeight = rBox.getHeight();
967
968     // Position of the left top corner
969     if( rLeftTop == "lefttop" )
970     {
971         left = xPos;
972         top = yPos;
973         refLeftTop = Position::kLeftTop;
974     }
975     else if( rLeftTop == "righttop" )
976     {
977         left = xPos - boxWidth + 1;
978         top = yPos;
979         refLeftTop = Position::kRightTop;
980     }
981     else if( rLeftTop == "leftbottom" )
982     {
983         left = xPos;
984         top = yPos - boxHeight + 1;
985         refLeftTop = Position::kLeftBottom;
986     }
987     else if( rLeftTop == "rightbottom" )
988     {
989         left = xPos - boxWidth + 1;
990         top = yPos - boxHeight + 1;
991         refLeftTop = Position::kRightBottom;
992     }
993
994     // Position of the right bottom corner
995     if( rRightBottom == "lefttop" )
996     {
997         right = xPos + width - 1;
998         bottom = yPos + height - 1;
999         refRightBottom = Position::kLeftTop;
1000     }
1001     else if( rRightBottom == "righttop" )
1002     {
1003         right = xPos + width - boxWidth;
1004         bottom = yPos + height - 1;
1005         refRightBottom = Position::kRightTop;
1006     }
1007     else if( rRightBottom == "leftbottom" )
1008     {
1009         right = xPos + width - 1;
1010         bottom = yPos + height - boxHeight;
1011         refRightBottom = Position::kLeftBottom;
1012     }
1013     else if( rRightBottom == "rightbottom" )
1014     {
1015         right = xPos + width - boxWidth;
1016         bottom = yPos + height - boxHeight;
1017         refRightBottom = Position::kRightBottom;
1018     }
1019
1020     return Position( left, top, right, bottom, rBox, refLeftTop,
1021                      refRightBottom );
1022 }
1023
1024
1025 GenericFont *Builder::getFont( const string &fontId )
1026 {
1027     GenericFont *pFont = m_pTheme->getFontById(fontId);
1028     if( !pFont && fontId == "defaultfont" )
1029     {
1030         // Get the resource path and try to load the default font
1031         OSFactory *pOSFactory = OSFactory::instance( getIntf() );
1032         const list<string> &resPath = pOSFactory->getResourcePath();
1033         const string &sep = pOSFactory->getDirSeparator();
1034
1035         list<string>::const_iterator it;
1036         for( it = resPath.begin(); it != resPath.end(); it++ )
1037         {
1038             string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
1039             pFont = new FT2Font( getIntf(), path, 12 );
1040             if( pFont->init() )
1041             {
1042                 // Font loaded successfully
1043                 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
1044                 break;
1045             }
1046             else
1047             {
1048                 delete pFont;
1049                 pFont = NULL;
1050             }
1051         }
1052         if( !pFont )
1053         {
1054             msg_Err( getIntf(), "failed to open the default font" );
1055         }
1056     }
1057     return pFont;
1058 }
1059
1060
1061 string Builder::getFilePath( const string &rFileName ) const
1062 {
1063     OSFactory *pFactory = OSFactory::instance( getIntf() );
1064     return m_path + pFactory->getDirSeparator() + sFromLocale( rFileName );
1065 }
1066
1067
1068
1069 Bezier *Builder::getPoints( const char *pTag ) const
1070 {
1071     vector<float> xBez, yBez;
1072     int x, y, n;
1073     while( 1 )
1074     {
1075         if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
1076         {
1077             return NULL;
1078         }
1079 #if 0
1080         if( x < 0 || y < 0 )
1081         {
1082             msg_Err( getIntf(),
1083                      "Slider points cannot have negative coordinates!" );
1084             return NULL;
1085         }
1086 #endif
1087         xBez.push_back( x );
1088         yBez.push_back( y );
1089         pTag += n;
1090         if( *pTag == '\0' )
1091         {
1092             break;
1093         }
1094         if( *(pTag++) != ',' )
1095         {
1096             return NULL;
1097         }
1098     }
1099
1100     // Create the Bezier curve
1101     return new Bezier( getIntf(), xBez, yBez );
1102 }
1103
1104
1105 uint32_t Builder::getColor( const string &rVal ) const
1106 {
1107     // Check it the value is a registered constant
1108     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1109     string val = pInterpreter->getConstant( rVal );
1110
1111     // Convert to an int value
1112     return SkinParser::convertColor( val.c_str() );
1113 }
1114