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