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