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