]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/builder.cpp
* skins2: new "xkeepratio" and "ykeepratio" attributes, common to all the
[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                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
435
436     pLayout->addControl( pButton, pos, rData.m_layer );
437
438     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
439 }
440
441
442 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
443 {
444     // Get the bitmaps of the checkbox
445     GenericBitmap *pBmpUp1 = NULL;
446     GET_BMP( pBmpUp1, rData.m_up1Id );
447
448     GenericBitmap *pBmpDown1 = pBmpUp1;
449     GET_BMP( pBmpDown1, rData.m_down1Id );
450
451     GenericBitmap *pBmpOver1 = pBmpUp1;
452     GET_BMP( pBmpOver1, rData.m_over1Id );
453
454     GenericBitmap *pBmpUp2 = NULL;
455     GET_BMP( pBmpUp2, rData.m_up2Id );
456
457     GenericBitmap *pBmpDown2 = pBmpUp2;
458     GET_BMP( pBmpDown2, rData.m_down2Id );
459
460     GenericBitmap *pBmpOver2 = pBmpUp2;
461     GET_BMP( pBmpOver2, rData.m_over2Id );
462
463     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
464     if( pLayout == NULL )
465     {
466         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
467         return;
468     }
469
470     CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
471     if( pCommand1 == NULL )
472     {
473         msg_Err( getIntf(), "invalid action: %s", rData.m_action1.c_str() );
474         return;
475     }
476
477     CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
478     if( pCommand2 == NULL )
479     {
480         msg_Err( getIntf(), "invalid action: %s", rData.m_action2.c_str() );
481         return;
482     }
483
484     // Get the state variable
485     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
486     VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
487     if( pVar == NULL )
488     {
489         // TODO: default state
490         return;
491     }
492
493     // Get the visibility variable
494     // XXX check when it is null
495     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
496
497     // Create the control
498     CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
499         *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
500         *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
501         UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
502         UString( getIntf(), rData.m_help.c_str() ), pVisible );
503
504     // Compute the position of the control
505     // XXX (we suppose all the images have the same size...)
506     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
507                                        rData.m_xPos, rData.m_yPos,
508                                        pBmpUp1->getWidth(),
509                                        pBmpUp1->getHeight(), *pLayout,
510                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
511
512     pLayout->addControl( pCheckbox, pos, rData.m_layer );
513
514     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
515 }
516
517
518 void Builder::addImage( const BuilderData::Image &rData )
519 {
520     GenericBitmap *pBmp = NULL;
521     GET_BMP( pBmp, rData.m_bmpId );
522
523     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
524     if( pLayout == NULL )
525     {
526         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
527         return;
528     }
529
530     TopWindow *pWindow = m_pTheme->getWindowById( rData.m_windowId );
531     if( pWindow == NULL )
532     {
533         msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
534         return;
535     }
536
537     CmdGeneric *pCommand = parseAction( rData.m_action2Id );
538     if( pCommand == NULL )
539     {
540         msg_Err( getIntf(), "invalid action: %s", rData.m_action2Id.c_str() );
541         return;
542     }
543
544     // Get the visibility variable
545     // XXX check when it is null
546     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
547     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
548
549     CtrlImage::resize_t resizeMethod =
550         (rData.m_resize == "scale" ? CtrlImage::kScale : CtrlImage::kMosaic);
551     CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, *pCommand,
552         resizeMethod, UString( getIntf(), rData.m_help.c_str() ), pVisible );
553
554     // Compute the position of the control
555     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
556                                        rData.m_xPos, rData.m_yPos,
557                                        pBmp->getWidth(), pBmp->getHeight(),
558                                        *pLayout, rData.m_xKeepRatio,
559                                        rData.m_yKeepRatio );
560
561     // XXX: test to be changed! XXX
562     if( rData.m_actionId == "move" )
563     {
564         CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
565              *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
566              pVisible );
567         pLayout->addControl( pMove, pos, rData.m_layer );
568     }
569     else if( rData.m_actionId == "resizeS" )
570     {
571         CtrlResize *pResize = new CtrlResize( getIntf(),
572                 m_pTheme->getWindowManager(), *pImage, *pLayout,
573                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
574                 WindowManager::kResizeS );
575         pLayout->addControl( pResize, pos, rData.m_layer );
576     }
577     else if( rData.m_actionId == "resizeE" )
578     {
579         CtrlResize *pResize = new CtrlResize( getIntf(),
580                 m_pTheme->getWindowManager(), *pImage, *pLayout,
581                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
582                 WindowManager::kResizeE );
583         pLayout->addControl( pResize, pos, rData.m_layer );
584     }
585     else if( rData.m_actionId == "resizeSE" )
586     {
587         CtrlResize *pResize = new CtrlResize( getIntf(),
588                 m_pTheme->getWindowManager(), *pImage, *pLayout,
589                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
590                 WindowManager::kResizeSE );
591         pLayout->addControl( pResize, pos, rData.m_layer );
592     }
593     else
594     {
595         pLayout->addControl( pImage, pos, rData.m_layer );
596     }
597
598     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
599 }
600
601
602 void Builder::addText( const BuilderData::Text &rData )
603 {
604     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
605     if( pLayout == NULL )
606     {
607         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
608         return;
609     }
610
611     GenericFont *pFont = getFont( rData.m_fontId );
612     if( pFont == NULL )
613     {
614         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
615         return;
616     }
617
618     // Convert the scrolling mode
619     CtrlText::Scrolling_t scrolling;
620     if( rData.m_scrolling == "auto" )
621         scrolling = CtrlText::kAutomatic;
622     else if( rData.m_scrolling == "manual" )
623         scrolling = CtrlText::kManual;
624     else if( rData.m_scrolling == "none" )
625         scrolling = CtrlText::kNone;
626     else
627     {
628         msg_Err( getIntf(), "invalid scrolling mode: %s",
629                  rData.m_scrolling.c_str() );
630         return;
631     }
632
633     // Convert the alignment
634     CtrlText::Align_t alignment;
635     if( rData.m_alignment == "left" )
636         alignment = CtrlText::kLeft;
637     else if( rData.m_alignment == "center" || rData.m_alignment == "centre" )
638         alignment = CtrlText::kCenter;
639     else if( rData.m_alignment == "right" )
640         alignment = CtrlText::kRight;
641     else
642     {
643         msg_Err( getIntf(), "invalid alignment: %s",
644                  rData.m_alignment.c_str() );
645         return;
646     }
647
648     // Create a text variable
649     VarText *pVar = new VarText( getIntf() );
650     m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
651
652     // Get the visibility variable
653     // XXX check when it is null
654     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
655     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
656
657     CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
658         UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible,
659         scrolling, alignment );
660
661     int height = pFont->getSize();
662
663     // Compute the position of the control
664     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
665                                        rData.m_xPos, rData.m_yPos,
666                                        rData.m_width, height, *pLayout,
667                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
668
669     pLayout->addControl( pText, pos, rData.m_layer );
670
671     // Set the text of the control
672     UString msg( getIntf(), rData.m_text.c_str() );
673     pVar->set( msg );
674
675     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
676 }
677
678
679 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
680 {
681     // Get the bitmaps of the slider
682     GenericBitmap *pSeq = NULL;
683     GET_BMP( pSeq, rData.m_sequence );
684
685     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
686     if( pLayout == NULL )
687     {
688         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
689         return;
690     }
691
692     // Get the variable associated to the slider
693     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
694     VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
695     if( pVar == NULL )
696     {
697         msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
698         return;
699     }
700
701     // Get the visibility variable
702     // XXX check when it is null
703     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
704
705     // Create the control
706     CtrlRadialSlider *pRadial =
707         new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
708                               rData.m_minAngle, rData.m_maxAngle,
709                               UString( getIntf(), rData.m_help.c_str() ),
710                               pVisible );
711
712     // XXX: resizing is not supported
713     // Compute the position of the control
714     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
715                                        rData.m_xPos, rData.m_yPos,
716                                        pSeq->getWidth(),
717                                        pSeq->getHeight() / rData.m_nbImages,
718                                        *pLayout,
719                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
720
721     pLayout->addControl( pRadial, pos, rData.m_layer );
722
723     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
724 }
725
726
727 void Builder::addSlider( const BuilderData::Slider &rData )
728 {
729     // Add the background first, so that we will still have something almost
730     // functional if the cursor cannot be created properly (this happens for
731     // some winamp2 skins, where the images of the cursor are not always
732     // present)
733
734     // Get the bitmaps of the background
735     GenericBitmap *pBgImage = NULL;
736     if( rData.m_imageId != "none" )
737     {
738         GET_BMP( pBgImage, rData.m_imageId );
739     }
740
741     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
742     if( pLayout == NULL )
743     {
744         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
745         return;
746     }
747
748     Bezier *pCurve = getPoints( rData.m_points.c_str() );
749     if( pCurve == NULL )
750     {
751         msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
752                  rData.m_points.c_str() );
753         return;
754     }
755     m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
756
757     // Get the visibility variable
758     // XXX check when it is null
759     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
760     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
761
762     // Get the variable associated to the slider
763     VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
764     if( pVar == NULL )
765     {
766         msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
767         return;
768     }
769
770     // Create the background control
771     CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(),
772         *pCurve, *pVar, rData.m_thickness, pBgImage, rData.m_nbHoriz,
773         rData.m_nbVert, rData.m_padHoriz, rData.m_padVert,
774         pVisible, UString( getIntf(), rData.m_help.c_str() ) );
775
776     // Compute the position of the control
777     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
778                                        rData.m_xPos, rData.m_yPos,
779                                        pCurve->getWidth(), pCurve->getHeight(),
780                                        *pLayout,
781                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
782
783     pLayout->addControl( pBackground, pos, rData.m_layer );
784
785     m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
786
787     // Get the bitmaps of the cursor
788     GenericBitmap *pBmpUp = NULL;
789     GET_BMP( pBmpUp, rData.m_upId );
790
791     GenericBitmap *pBmpDown = pBmpUp;
792     GET_BMP( pBmpDown, rData.m_downId );
793
794     GenericBitmap *pBmpOver = pBmpUp;
795     GET_BMP( pBmpOver, rData.m_overId );
796
797     // Create the cursor control
798     CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
799         *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
800         UString( getIntf(), rData.m_tooltip.c_str() ),
801         UString( getIntf(), rData.m_help.c_str() ) );
802
803     pLayout->addControl( pCursor, pos, rData.m_layer );
804
805     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
806
807     // Associate the cursor to the background
808     pBackground->associateCursor( *pCursor );
809 }
810
811
812 void Builder::addList( const BuilderData::List &rData )
813 {
814     // Get the background bitmap, if any
815     GenericBitmap *pBgBmp = NULL;
816     GET_BMP( pBgBmp, rData.m_bgImageId );
817
818     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
819     if( pLayout == NULL )
820     {
821         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
822         return;
823     }
824
825     GenericFont *pFont = getFont( rData.m_fontId );
826     if( pFont == NULL )
827     {
828         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
829         return;
830     }
831
832     // Get the list variable
833     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
834     VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
835     if( pVar == NULL )
836     {
837         msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
838         return;
839     }
840
841     // Get the visibility variable
842     // XXX check when it is null
843     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
844
845     // Get the color values
846     uint32_t fgColor = getColor( rData.m_fgColor );
847     uint32_t playColor = getColor( rData.m_playColor );
848     uint32_t bgColor1 = getColor( rData.m_bgColor1 );
849     uint32_t bgColor2 = getColor( rData.m_bgColor2 );
850     uint32_t selColor = getColor( rData.m_selColor );
851
852     // Create the list control
853     CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
854        fgColor, playColor, bgColor1, bgColor2, selColor,
855        UString( getIntf(), rData.m_help.c_str() ), pVisible );
856
857     // Compute the position of the control
858     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
859                                        rData.m_xPos, rData.m_yPos,
860                                        rData.m_width, rData.m_height,
861                                        *pLayout,
862                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
863
864     pLayout->addControl( pList, pos, rData.m_layer );
865
866     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
867 }
868
869 void Builder::addTree( const BuilderData::Tree &rData )
870 {
871     // Get the bitmaps, if any
872     GenericBitmap *pBgBmp = NULL;
873     GenericBitmap *pItemBmp = NULL;
874     GenericBitmap *pOpenBmp = NULL;
875     GenericBitmap *pClosedBmp = NULL;
876     GET_BMP( pBgBmp, rData.m_bgImageId );
877     GET_BMP( pItemBmp, rData.m_itemImageId );
878     GET_BMP( pOpenBmp, rData.m_openImageId );
879     GET_BMP( pClosedBmp, rData.m_closedImageId );
880
881     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
882     if( pLayout == NULL )
883     {
884         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
885         return;
886     }
887
888     GenericFont *pFont = getFont( rData.m_fontId );
889     if( pFont == NULL )
890     {
891         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
892         return;
893     }
894
895     // Get the list variable
896     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
897     VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
898     if( pVar == NULL )
899     {
900         msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
901         return;
902     }
903
904     // Get the visibility variable
905     // XXX check when it is null
906     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
907     VarBool *pFlat = pInterpreter->getVarBool( rData.m_flat, m_pTheme );
908
909     // Get the color values
910     uint32_t fgColor = getColor( rData.m_fgColor );
911     uint32_t playColor = getColor( rData.m_playColor );
912     uint32_t bgColor1 = getColor( rData.m_bgColor1 );
913     uint32_t bgColor2 = getColor( rData.m_bgColor2 );
914     uint32_t selColor = getColor( rData.m_selColor );
915
916     // Create the list control
917     CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
918        pItemBmp, pOpenBmp, pClosedBmp,
919        fgColor, playColor, bgColor1, bgColor2, selColor,
920        UString( getIntf(), rData.m_help.c_str() ), pVisible, pFlat );
921
922     // Compute the position of the control
923     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
924                                        rData.m_xPos, rData.m_yPos,
925                                        rData.m_width, rData.m_height,
926                                        *pLayout,
927                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
928
929     pLayout->addControl( pTree, pos, rData.m_layer );
930
931     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
932 }
933
934 void Builder::addVideo( const BuilderData::Video &rData )
935 {
936     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
937     if( pLayout == NULL )
938     {
939         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
940         return;
941     }
942
943     // Get the visibility variable
944     // XXX check when it is null
945     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
946     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
947
948     CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
949         rData.m_autoResize, UString( getIntf(), rData.m_help.c_str() ),
950         pVisible );
951
952     // Compute the position of the control
953     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
954                                        rData.m_xPos, rData.m_yPos,
955                                        rData.m_width, rData.m_height,
956                                        *pLayout,
957                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
958
959     pLayout->addControl( pVideo, pos, rData.m_layer );
960
961     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
962 }
963
964
965 const Position Builder::makePosition( const string &rLeftTop,
966                                       const string &rRightBottom,
967                                       int xPos, int yPos, int width,
968                                       int height, const Box &rBox,
969                                       bool xKeepRatio, bool yKeepRatio ) const
970 {
971     int left = 0, top = 0, right = 0, bottom = 0;
972     Position::Ref_t refLeftTop = Position::kLeftTop;
973     Position::Ref_t refRightBottom = Position::kLeftTop;
974
975     int boxWidth = rBox.getWidth();
976     int boxHeight = rBox.getHeight();
977
978     // Position of the left top corner
979     if( rLeftTop == "lefttop" )
980     {
981         left = xPos;
982         top = yPos;
983         refLeftTop = Position::kLeftTop;
984     }
985     else if( rLeftTop == "righttop" )
986     {
987         left = xPos - boxWidth + 1;
988         top = yPos;
989         refLeftTop = Position::kRightTop;
990     }
991     else if( rLeftTop == "leftbottom" )
992     {
993         left = xPos;
994         top = yPos - boxHeight + 1;
995         refLeftTop = Position::kLeftBottom;
996     }
997     else if( rLeftTop == "rightbottom" )
998     {
999         left = xPos - boxWidth + 1;
1000         top = yPos - boxHeight + 1;
1001         refLeftTop = Position::kRightBottom;
1002     }
1003
1004     // Position of the right bottom corner
1005     if( rRightBottom == "lefttop" )
1006     {
1007         right = xPos + width - 1;
1008         bottom = yPos + height - 1;
1009         refRightBottom = Position::kLeftTop;
1010     }
1011     else if( rRightBottom == "righttop" )
1012     {
1013         right = xPos + width - boxWidth;
1014         bottom = yPos + height - 1;
1015         refRightBottom = Position::kRightTop;
1016     }
1017     else if( rRightBottom == "leftbottom" )
1018     {
1019         right = xPos + width - 1;
1020         bottom = yPos + height - boxHeight;
1021         refRightBottom = Position::kLeftBottom;
1022     }
1023     else if( rRightBottom == "rightbottom" )
1024     {
1025         right = xPos + width - boxWidth;
1026         bottom = yPos + height - boxHeight;
1027         refRightBottom = Position::kRightBottom;
1028     }
1029
1030     return Position( left, top, right, bottom, rBox, refLeftTop,
1031                      refRightBottom, xKeepRatio, yKeepRatio );
1032 }
1033
1034
1035 GenericFont *Builder::getFont( const string &fontId )
1036 {
1037     GenericFont *pFont = m_pTheme->getFontById(fontId);
1038     if( !pFont && fontId == "defaultfont" )
1039     {
1040         // Get the resource path and try to load the default font
1041         OSFactory *pOSFactory = OSFactory::instance( getIntf() );
1042         const list<string> &resPath = pOSFactory->getResourcePath();
1043         const string &sep = pOSFactory->getDirSeparator();
1044
1045         list<string>::const_iterator it;
1046         for( it = resPath.begin(); it != resPath.end(); it++ )
1047         {
1048             string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
1049             pFont = new FT2Font( getIntf(), path, 12 );
1050             if( pFont->init() )
1051             {
1052                 // Font loaded successfully
1053                 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
1054                 break;
1055             }
1056             else
1057             {
1058                 delete pFont;
1059                 pFont = NULL;
1060             }
1061         }
1062         if( !pFont )
1063         {
1064             msg_Err( getIntf(), "failed to open the default font" );
1065         }
1066     }
1067     return pFont;
1068 }
1069
1070
1071 string Builder::getFilePath( const string &rFileName ) const
1072 {
1073     OSFactory *pFactory = OSFactory::instance( getIntf() );
1074     return m_path + pFactory->getDirSeparator() + sFromLocale( rFileName );
1075 }
1076
1077
1078
1079 Bezier *Builder::getPoints( const char *pTag ) const
1080 {
1081     vector<float> xBez, yBez;
1082     int x, y, n;
1083     while( 1 )
1084     {
1085         if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
1086         {
1087             return NULL;
1088         }
1089 #if 0
1090         if( x < 0 || y < 0 )
1091         {
1092             msg_Err( getIntf(),
1093                      "Slider points cannot have negative coordinates!" );
1094             return NULL;
1095         }
1096 #endif
1097         xBez.push_back( x );
1098         yBez.push_back( y );
1099         pTag += n;
1100         if( *pTag == '\0' )
1101         {
1102             break;
1103         }
1104         if( *(pTag++) != ',' )
1105         {
1106             return NULL;
1107         }
1108     }
1109
1110     // Create the Bezier curve
1111     return new Bezier( getIntf(), xBez, yBez );
1112 }
1113
1114
1115 uint32_t Builder::getColor( const string &rVal ) const
1116 {
1117     // Check it the value is a registered constant
1118     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1119     string val = pInterpreter->getConstant( rVal );
1120
1121     // Convert to an int value
1122     return SkinParser::convertColor( val.c_str() );
1123 }
1124