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