]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/builder.cpp
skins2: implement art display in image controls
[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 :
578         rData.m_resize == "mosaic" ? CtrlImage::kMosaic :
579                                      CtrlImage::kScaleAndRatioPreserved;
580     CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, *pCommand,
581         resizeMethod, UString( getIntf(), rData.m_help.c_str() ), pVisible,
582         rData.m_art );
583     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
584
585     // Compute the position of the control
586     const GenericRect *pRect;
587     GET_BOX( pRect, rData.m_panelId , pLayout);
588     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
589                                        rData.m_xPos, rData.m_yPos,
590                                        pBmp->getWidth(), pBmp->getHeight(),
591                                        *pRect, rData.m_xKeepRatio,
592                                        rData.m_yKeepRatio );
593
594     if( rData.m_actionId == "move" )
595     {
596         CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
597              *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
598              pVisible );
599         m_pTheme->m_controls[rData.m_id + "_move"] = CtrlGenericPtr( pMove );
600         pLayout->addControl( pMove, pos, rData.m_layer );
601     }
602     else if( rData.m_actionId == "resizeS" )
603     {
604         CtrlResize *pResize = new CtrlResize( getIntf(),
605                 m_pTheme->getWindowManager(), *pImage, *pLayout,
606                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
607                 WindowManager::kResizeS );
608         m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
609         pLayout->addControl( pResize, pos, rData.m_layer );
610     }
611     else if( rData.m_actionId == "resizeE" )
612     {
613         CtrlResize *pResize = new CtrlResize( getIntf(),
614                 m_pTheme->getWindowManager(), *pImage, *pLayout,
615                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
616                 WindowManager::kResizeE );
617         m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
618         pLayout->addControl( pResize, pos, rData.m_layer );
619     }
620     else if( rData.m_actionId == "resizeSE" )
621     {
622         CtrlResize *pResize = new CtrlResize( getIntf(),
623                 m_pTheme->getWindowManager(), *pImage, *pLayout,
624                 UString( getIntf(), rData.m_help.c_str() ), pVisible,
625                 WindowManager::kResizeSE );
626         m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
627         pLayout->addControl( pResize, pos, rData.m_layer );
628     }
629     else
630     {
631         pLayout->addControl( pImage, pos, rData.m_layer );
632     }
633 }
634
635
636 void Builder::addPanel( const BuilderData::Panel &rData )
637 {
638     // This method makes the assumption that the Panels are created in the
639     // order of the XML, because each child Panel expects its parent Panel
640     // in order to be fully created
641
642     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
643     if( pLayout == NULL )
644     {
645         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
646         return;
647     }
648
649     const GenericRect *pRect;
650     GET_BOX( pRect, rData.m_panelId , pLayout);
651     Position *pPos =
652         new Position( makePosition( rData.m_leftTop, rData.m_rightBottom,
653                                     rData.m_xPos, rData.m_yPos,
654                                     rData.m_width, rData.m_height,
655                                     *pRect, rData.m_xKeepRatio,
656                                     rData.m_yKeepRatio ) );
657     m_pTheme->m_positions[rData.m_id] = PositionPtr( pPos );
658 }
659
660
661 void Builder::addText( const BuilderData::Text &rData )
662 {
663     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
664     if( pLayout == NULL )
665     {
666         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
667         return;
668     }
669
670     GenericFont *pFont = getFont( rData.m_fontId );
671     if( pFont == NULL )
672     {
673         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
674         return;
675     }
676
677     // Convert the scrolling mode
678     CtrlText::Scrolling_t scrolling;
679     if( rData.m_scrolling == "auto" )
680         scrolling = CtrlText::kAutomatic;
681     else if( rData.m_scrolling == "manual" )
682         scrolling = CtrlText::kManual;
683     else if( rData.m_scrolling == "none" )
684         scrolling = CtrlText::kNone;
685     else
686     {
687         msg_Err( getIntf(), "invalid scrolling mode: %s",
688                  rData.m_scrolling.c_str() );
689         return;
690     }
691
692     // Convert the alignment
693     CtrlText::Align_t alignment;
694     if( rData.m_alignment == "left" )
695         alignment = CtrlText::kLeft;
696     else if( rData.m_alignment == "center" || rData.m_alignment == "centre" )
697         alignment = CtrlText::kCenter;
698     else if( rData.m_alignment == "right" )
699         alignment = CtrlText::kRight;
700     else
701     {
702         msg_Err( getIntf(), "invalid alignment: %s",
703                  rData.m_alignment.c_str() );
704         return;
705     }
706
707     // Create a text variable
708     VarText *pVar = new VarText( getIntf() );
709     m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
710
711     // Get the visibility variable
712     // XXX check when it is null
713     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
714     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
715
716     CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
717         UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible,
718         scrolling, alignment );
719     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
720
721     int height = pFont->getSize();
722
723     // Compute the position of the control
724     const GenericRect *pRect;
725     GET_BOX( pRect, rData.m_panelId , pLayout);
726     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
727                                        rData.m_xPos, rData.m_yPos,
728                                        rData.m_width, height, *pRect,
729                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
730
731     pLayout->addControl( pText, pos, rData.m_layer );
732
733     // Set the text of the control
734     UString msg( getIntf(), rData.m_text.c_str() );
735     pVar->set( msg );
736 }
737
738
739 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
740 {
741     // Get the bitmaps of the slider
742     GenericBitmap *pSeq = NULL;
743     GET_BMP( pSeq, rData.m_sequence );
744
745     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
746     if( pLayout == NULL )
747     {
748         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
749         return;
750     }
751
752     // Get the variable associated to the slider
753     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
754     VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
755     if( pVar == NULL )
756     {
757         msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
758         return;
759     }
760
761     // Get the visibility variable
762     // XXX check when it is null
763     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
764
765     // Create the control
766     CtrlRadialSlider *pRadial =
767         new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
768                               rData.m_minAngle, rData.m_maxAngle,
769                               UString( getIntf(), rData.m_help.c_str() ),
770                               pVisible );
771     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
772
773     // XXX: resizing is not supported
774     // Compute the position of the control
775     const GenericRect *pRect;
776     GET_BOX( pRect, rData.m_panelId , pLayout);
777     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
778                                        rData.m_xPos, rData.m_yPos,
779                                        pSeq->getWidth(),
780                                        pSeq->getHeight() / rData.m_nbImages,
781                                        *pRect,
782                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
783
784     pLayout->addControl( pRadial, pos, rData.m_layer );
785 }
786
787
788 void Builder::addSlider( const BuilderData::Slider &rData )
789 {
790     // Add the background first, so that we will still have something almost
791     // functional if the cursor cannot be created properly (this happens for
792     // some winamp2 skins, where the images of the cursor are not always
793     // present)
794
795     // Get the bitmaps of the background
796     GenericBitmap *pBgImage = NULL;
797     if( rData.m_imageId != "none" )
798     {
799         GET_BMP( pBgImage, rData.m_imageId );
800     }
801
802     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
803     if( pLayout == NULL )
804     {
805         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
806         return;
807     }
808
809     Bezier *pCurve = getPoints( rData.m_points.c_str() );
810     if( pCurve == NULL )
811     {
812         msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
813                  rData.m_points.c_str() );
814         return;
815     }
816     m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
817
818     // Get the visibility variable
819     // XXX check when it is null
820     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
821     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
822
823     // Get the variable associated to the slider
824     VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
825     if( pVar == NULL )
826     {
827         msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
828         return;
829     }
830
831     // Create the background control
832     CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(),
833         *pCurve, *pVar, rData.m_thickness, pBgImage, rData.m_nbHoriz,
834         rData.m_nbVert, rData.m_padHoriz, rData.m_padVert,
835         pVisible, UString( getIntf(), rData.m_help.c_str() ) );
836     m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
837
838     // Compute the position of the control
839     const GenericRect *pRect;
840     GET_BOX( pRect, rData.m_panelId , pLayout);
841     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
842                                        rData.m_xPos, rData.m_yPos,
843                                        pCurve->getWidth(), pCurve->getHeight(),
844                                        *pRect,
845                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
846
847     pLayout->addControl( pBackground, pos, rData.m_layer );
848
849     // Get the bitmaps of the cursor
850     GenericBitmap *pBmpUp = NULL;
851     GET_BMP( pBmpUp, rData.m_upId );
852
853     GenericBitmap *pBmpDown = pBmpUp;
854     GET_BMP( pBmpDown, rData.m_downId );
855
856     GenericBitmap *pBmpOver = pBmpUp;
857     GET_BMP( pBmpOver, rData.m_overId );
858
859     // Create the cursor control
860     CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
861         *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
862         UString( getIntf(), rData.m_tooltip.c_str() ),
863         UString( getIntf(), rData.m_help.c_str() ) );
864     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
865
866     pLayout->addControl( pCursor, pos, rData.m_layer );
867
868     // Associate the cursor to the background
869     pBackground->associateCursor( *pCursor );
870 }
871
872
873 void Builder::addList( const BuilderData::List &rData )
874 {
875     // Get the background bitmap, if any
876     GenericBitmap *pBgBmp = NULL;
877     GET_BMP( pBgBmp, rData.m_bgImageId );
878
879     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
880     if( pLayout == NULL )
881     {
882         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
883         return;
884     }
885
886     GenericFont *pFont = getFont( rData.m_fontId );
887     if( pFont == NULL )
888     {
889         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
890         return;
891     }
892
893     // Get the list variable
894     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
895     VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
896     if( pVar == NULL )
897     {
898         msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
899         return;
900     }
901
902     // Get the visibility variable
903     // XXX check when it is null
904     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
905
906     // Get the color values
907     uint32_t fgColor = getColor( rData.m_fgColor );
908     uint32_t playColor = getColor( rData.m_playColor );
909     uint32_t bgColor1 = getColor( rData.m_bgColor1 );
910     uint32_t bgColor2 = getColor( rData.m_bgColor2 );
911     uint32_t selColor = getColor( rData.m_selColor );
912
913     // Create the list control
914     CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
915        fgColor, playColor, bgColor1, bgColor2, selColor,
916        UString( getIntf(), rData.m_help.c_str() ), pVisible );
917     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
918
919     // Compute the position of the control
920     const GenericRect *pRect;
921     GET_BOX( pRect, rData.m_panelId , pLayout);
922     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
923                                        rData.m_xPos, rData.m_yPos,
924                                        rData.m_width, rData.m_height,
925                                        *pRect,
926                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
927
928     pLayout->addControl( pList, pos, rData.m_layer );
929 }
930
931 void Builder::addTree( const BuilderData::Tree &rData )
932 {
933     // Get the bitmaps, if any
934     GenericBitmap *pBgBmp = NULL;
935     GenericBitmap *pItemBmp = NULL;
936     GenericBitmap *pOpenBmp = NULL;
937     GenericBitmap *pClosedBmp = NULL;
938     GET_BMP( pBgBmp, rData.m_bgImageId );
939     GET_BMP( pItemBmp, rData.m_itemImageId );
940     GET_BMP( pOpenBmp, rData.m_openImageId );
941     GET_BMP( pClosedBmp, rData.m_closedImageId );
942
943     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
944     if( pLayout == NULL )
945     {
946         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
947         return;
948     }
949
950     GenericFont *pFont = getFont( rData.m_fontId );
951     if( pFont == NULL )
952     {
953         msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
954         return;
955     }
956
957     // Get the list variable
958     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
959     VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
960     if( pVar == NULL )
961     {
962         msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
963         return;
964     }
965
966     // Get the visibility variable
967     // XXX check when it is null
968     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
969     VarBool *pFlat = pInterpreter->getVarBool( rData.m_flat, m_pTheme );
970
971     // Get the color values
972     uint32_t fgColor = getColor( rData.m_fgColor );
973     uint32_t playColor = getColor( rData.m_playColor );
974     uint32_t bgColor1 = getColor( rData.m_bgColor1 );
975     uint32_t bgColor2 = getColor( rData.m_bgColor2 );
976     uint32_t selColor = getColor( rData.m_selColor );
977
978     // Create the list control
979     CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
980        pItemBmp, pOpenBmp, pClosedBmp,
981        fgColor, playColor, bgColor1, bgColor2, selColor,
982        UString( getIntf(), rData.m_help.c_str() ), pVisible, pFlat );
983     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
984
985     // Compute the position of the control
986     const GenericRect *pRect;
987     GET_BOX( pRect, rData.m_panelId , pLayout);
988     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
989                                        rData.m_xPos, rData.m_yPos,
990                                        rData.m_width, rData.m_height,
991                                        *pRect,
992                                        rData.m_xKeepRatio, rData.m_yKeepRatio );
993
994     pLayout->addControl( pTree, pos, rData.m_layer );
995 }
996
997 void Builder::addVideo( const BuilderData::Video &rData )
998 {
999     GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
1000     if( pLayout == NULL )
1001     {
1002         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
1003         return;
1004     }
1005
1006     // Get the visibility variable
1007     // XXX check when it is null
1008     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1009     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
1010
1011     CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
1012         rData.m_autoResize, UString( getIntf(), rData.m_help.c_str() ),
1013         pVisible );
1014     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
1015
1016     // if autoresize is true, force the control to resize
1017     BuilderData::Video Data = rData;
1018     if( rData.m_autoResize )
1019     {
1020         Data.m_leftTop = "lefttop";
1021         Data.m_rightBottom = "rightbottom";
1022     }
1023
1024     // Compute the position of the control
1025     const GenericRect *pRect;
1026     GET_BOX( pRect, rData.m_panelId , pLayout);
1027     const Position pos = makePosition( Data.m_leftTop, Data.m_rightBottom,
1028                                        Data.m_xPos, Data.m_yPos,
1029                                        Data.m_width, Data.m_height,
1030                                        *pRect,
1031                                        Data.m_xKeepRatio, Data.m_yKeepRatio );
1032
1033     pLayout->addControl( pVideo, pos, rData.m_layer );
1034 }
1035
1036
1037 const Position Builder::makePosition( const string &rLeftTop,
1038                                       const string &rRightBottom,
1039                                       int xPos, int yPos, int width,
1040                                       int height, const GenericRect &rRect,
1041                                       bool xKeepRatio, bool yKeepRatio ) const
1042 {
1043     int left = 0, top = 0, right = 0, bottom = 0;
1044     Position::Ref_t refLeftTop = Position::kLeftTop;
1045     Position::Ref_t refRightBottom = Position::kLeftTop;
1046
1047     int boxWidth = rRect.getWidth();
1048     int boxHeight = rRect.getHeight();
1049
1050     // Position of the left top corner
1051     if( rLeftTop == "lefttop" )
1052     {
1053         left = xPos;
1054         top = yPos;
1055         refLeftTop = Position::kLeftTop;
1056     }
1057     else if( rLeftTop == "righttop" )
1058     {
1059         left = xPos - boxWidth + 1;
1060         top = yPos;
1061         refLeftTop = Position::kRightTop;
1062     }
1063     else if( rLeftTop == "leftbottom" )
1064     {
1065         left = xPos;
1066         top = yPos - boxHeight + 1;
1067         refLeftTop = Position::kLeftBottom;
1068     }
1069     else if( rLeftTop == "rightbottom" )
1070     {
1071         left = xPos - boxWidth + 1;
1072         top = yPos - boxHeight + 1;
1073         refLeftTop = Position::kRightBottom;
1074     }
1075
1076     // Position of the right bottom corner
1077     if( rRightBottom == "lefttop" )
1078     {
1079         right = xPos + width - 1;
1080         bottom = yPos + height - 1;
1081         refRightBottom = Position::kLeftTop;
1082     }
1083     else if( rRightBottom == "righttop" )
1084     {
1085         right = xPos + width - boxWidth;
1086         bottom = yPos + height - 1;
1087         refRightBottom = Position::kRightTop;
1088     }
1089     else if( rRightBottom == "leftbottom" )
1090     {
1091         right = xPos + width - 1;
1092         bottom = yPos + height - boxHeight;
1093         refRightBottom = Position::kLeftBottom;
1094     }
1095     else if( rRightBottom == "rightbottom" )
1096     {
1097         right = xPos + width - boxWidth;
1098         bottom = yPos + height - boxHeight;
1099         refRightBottom = Position::kRightBottom;
1100     }
1101
1102     // In "keep ratio" mode, overwrite the previously computed values with the
1103     // actual ones
1104     // XXX: this coupling between makePosition and the Position class should
1105     // be reduced...
1106     if( xKeepRatio )
1107     {
1108         left = xPos;
1109         right = xPos + width;
1110     }
1111     if( yKeepRatio )
1112     {
1113         top = yPos;
1114         bottom = yPos + height;
1115     }
1116
1117     return Position( left, top, right, bottom, rRect, refLeftTop,
1118                      refRightBottom, xKeepRatio, yKeepRatio );
1119 }
1120
1121
1122 GenericFont *Builder::getFont( const string &fontId )
1123 {
1124     GenericFont *pFont = m_pTheme->getFontById(fontId);
1125     if( !pFont && fontId == "defaultfont" )
1126     {
1127         // Get the resource path and try to load the default font
1128         OSFactory *pOSFactory = OSFactory::instance( getIntf() );
1129         const list<string> &resPath = pOSFactory->getResourcePath();
1130         const string &sep = pOSFactory->getDirSeparator();
1131
1132         list<string>::const_iterator it;
1133         for( it = resPath.begin(); it != resPath.end(); it++ )
1134         {
1135             string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
1136             pFont = new FT2Font( getIntf(), path, 12 );
1137             if( pFont->init() )
1138             {
1139                 // Font loaded successfully
1140                 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
1141                 break;
1142             }
1143             else
1144             {
1145                 delete pFont;
1146                 pFont = NULL;
1147             }
1148         }
1149         if( !pFont )
1150         {
1151             msg_Err( getIntf(), "failed to open the default font" );
1152         }
1153     }
1154     return pFont;
1155 }
1156
1157
1158 string Builder::getFilePath( const string &rFileName ) const
1159 {
1160     OSFactory *pFactory = OSFactory::instance( getIntf() );
1161     const string &sep = pFactory->getDirSeparator();
1162
1163     string file = rFileName;
1164     if( file.find( "\\" ) != string::npos )
1165     {
1166         // For skins to be valid on both Linux and Win32,
1167         // slash should be used as path separator for both OSs.
1168         msg_Warn( getIntf(), "use of '/' is preferred to '\\' for paths" );
1169         int pos;
1170         while( ( pos = file.find( "\\" ) ) != string::npos )
1171            file[pos] = '/';
1172     }
1173
1174 #ifdef WIN32
1175     int pos;
1176     while( ( pos = file.find( "/" ) ) != string::npos )
1177        file.replace( pos, 1, sep );
1178 #endif
1179
1180     return m_path + sep + sFromLocale( file );
1181 }
1182
1183
1184
1185 Bezier *Builder::getPoints( const char *pTag ) const
1186 {
1187     vector<float> xBez, yBez;
1188     int x, y, n;
1189     while( 1 )
1190     {
1191         if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
1192         {
1193             return NULL;
1194         }
1195
1196         xBez.push_back( x );
1197         yBez.push_back( y );
1198         pTag += n;
1199         if( *pTag == '\0' )
1200         {
1201             break;
1202         }
1203         if( *(pTag++) != ',' )
1204         {
1205             return NULL;
1206         }
1207     }
1208
1209     // Create the Bezier curve
1210     return new Bezier( getIntf(), xBez, yBez );
1211 }
1212
1213
1214 uint32_t Builder::getColor( const string &rVal ) const
1215 {
1216     // Check it the value is a registered constant
1217     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1218     string val = pInterpreter->getConstant( rVal );
1219
1220     // Convert to an int value
1221     return SkinParser::convertColor( val.c_str() );
1222 }
1223