]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/builder.cpp
* winamp2.xml: hack to support nums_ex.bmp and numbers.bmp
[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., 59 Temple Place - Suite 330, Boston, MA  02111, 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/theme.hpp"
37 #include "../controls/ctrl_button.hpp"
38 #include "../controls/ctrl_checkbox.hpp"
39 #include "../controls/ctrl_image.hpp"
40 #include "../controls/ctrl_list.hpp"
41 #include "../controls/ctrl_move.hpp"
42 #include "../controls/ctrl_resize.hpp"
43 #include "../controls/ctrl_slider.hpp"
44 #include "../controls/ctrl_radialslider.hpp"
45 #include "../controls/ctrl_text.hpp"
46 #include "../controls/ctrl_tree.hpp"
47 #include "../controls/ctrl_video.hpp"
48 #include "../utils/position.hpp"
49 #include "../utils/var_bool.hpp"
50 #include "../utils/var_text.hpp"
51
52 #include "vlc_image.h"
53
54
55 Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData ):
56     SkinObject( pIntf ), m_rData( rData ), m_pTheme( NULL )
57 {
58     m_pImageHandler = image_HandlerCreate( pIntf );
59 }
60
61 Builder::~Builder()
62 {
63     if( m_pImageHandler ) image_HandlerDelete( m_pImageHandler );
64 }
65
66 CmdGeneric *Builder::parseAction( const string &rAction )
67 {
68     return Interpreter::instance( getIntf() )->parseAction( rAction, m_pTheme );
69 }
70
71
72 // Useful macro
73 #define ADD_OBJECTS( type ) \
74     list<BuilderData::type>::const_iterator it##type; \
75     for( it##type = m_rData.m_list##type.begin(); \
76          it##type != m_rData.m_list##type.end(); it##type++ ) \
77     { \
78         add##type( *it##type ); \
79     }
80
81
82 Theme *Builder::build()
83 {
84     m_pTheme = new Theme( getIntf() );
85     if( m_pTheme == NULL )
86     {
87         return NULL;
88     }
89
90     // Create everything from the data in the XML
91     ADD_OBJECTS( Theme );
92     ADD_OBJECTS( Bitmap );
93     ADD_OBJECTS( SubBitmap );
94     ADD_OBJECTS( BitmapFont );
95     ADD_OBJECTS( Font );
96     ADD_OBJECTS( Window );
97     ADD_OBJECTS( Layout );
98     ADD_OBJECTS( Anchor );
99     ADD_OBJECTS( Button );
100     ADD_OBJECTS( Checkbox );
101     ADD_OBJECTS( Image );
102     ADD_OBJECTS( Text );
103     ADD_OBJECTS( RadialSlider );
104     ADD_OBJECTS( Slider );
105     ADD_OBJECTS( List );
106     ADD_OBJECTS( Tree );
107     ADD_OBJECTS( Video );
108
109     return m_pTheme;
110 }
111
112
113 // Macro to get a bitmap by its ID in the builder
114 #define GET_BMP( pBmp, id ) \
115     if( id != "none" ) \
116     { \
117         pBmp = m_pTheme->getBitmapById(id); \
118         if( pBmp == NULL ) \
119         { \
120             msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
121             return; \
122         } \
123     }
124
125 void Builder::addTheme( const BuilderData::Theme &rData )
126 {
127     WindowManager &rManager = m_pTheme->getWindowManager();
128     rManager.setMagnetValue( rData.m_magnet );
129     rManager.setAlphaValue( rData.m_alpha );
130     rManager.setMoveAlphaValue( rData.m_moveAlpha );
131     GenericFont *pFont = getFont( rData.m_tooltipfont );
132     if( pFont )
133     {
134         rManager.createTooltip( *pFont );
135     }
136     else
137     {
138         msg_Warn( getIntf(), "Invalid tooltip font: %s",
139                   rData.m_tooltipfont.c_str() );
140     }
141 }
142
143
144 void Builder::addBitmap( const BuilderData::Bitmap &rData )
145 {
146     GenericBitmap *pBmp =
147         new FileBitmap( getIntf(), m_pImageHandler,
148                         rData.m_fileName, rData.m_alphaColor );
149     if( !pBmp->getData() )
150     {
151         // Invalid bitmap
152         delete pBmp;
153         return;
154     }
155     m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
156 }
157
158
159 void Builder::addSubBitmap( const BuilderData::SubBitmap &rData )
160 {
161     // Get the parent bitmap
162     GenericBitmap *pParentBmp = NULL;
163     GET_BMP( pParentBmp, rData.m_parent );
164     if( !pParentBmp )
165     {
166         msg_Err( getIntf(), "unknown bitmap id: %s", rData.m_parent.c_str() );
167         return;
168     }
169
170     // Copy a region of the parent bitmap to the new one
171     BitmapImpl *pBmp =
172         new BitmapImpl( getIntf(), rData.m_width, rData.m_height );
173     pBmp->drawBitmap( *pParentBmp, rData.m_x, rData.m_y, 0, 0, rData.m_width,
174                       rData.m_height );
175
176     m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
177 }
178
179
180 void Builder::addBitmapFont( const BuilderData::BitmapFont &rData )
181 {
182     GenericBitmap *pBmp =
183         new FileBitmap( getIntf(), m_pImageHandler, rData.m_file, 0 );
184     if( !pBmp->getData() )
185     {
186         // invalid bitmap
187         delete pBmp;
188         return;
189     }
190
191     m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
192
193     GenericFont *pFont = new BitmapFont( getIntf(), *pBmp, rData.m_type );
194     if( pFont->init() )
195     {
196         m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
197     }
198     else
199     {
200         delete pFont;
201     }
202 }
203
204
205 void Builder::addFont( const BuilderData::Font &rData )
206 {
207     GenericFont *pFont = new FT2Font( getIntf(), rData.m_fontFile,
208                                       rData.m_size );
209     if( pFont->init() )
210     {
211         m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
212     }
213     else
214     {
215         delete pFont;
216     }
217 }
218
219
220 void Builder::addWindow( const BuilderData::Window &rData )
221 {
222     TopWindow *pWin =
223         new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
224                        m_pTheme->getWindowManager(),
225                        rData.m_dragDrop, rData.m_playOnDrop,
226                        rData.m_visible );
227
228     m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
229 }
230
231
232 void Builder::addLayout( const BuilderData::Layout &rData )
233 {
234     TopWindow *pWin = m_pTheme->getWindowById(rData.m_windowId);
235     if( pWin == NULL )
236     {
237         msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
238         return;
239     }
240
241     int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
242     int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
243     int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
244                     rData.m_height;
245     int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
246                     rData.m_height;
247     GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
248                                                 rData.m_height,
249                                                 minWidth, maxWidth, minHeight,
250                                                 maxHeight );
251     m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
252
253     // Attach the layout to its window
254     m_pTheme->getWindowManager().addLayout( *pWin, *pLayout );
255 }
256
257
258 void Builder::addAnchor( const BuilderData::Anchor &rData )
259 {
260     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
261     if( pLayout == NULL )
262     {
263         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
264         return;
265     }
266
267     Bezier *pCurve = getPoints( rData.m_points.c_str() );
268     if( pCurve == NULL )
269     {
270         msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
271                  rData.m_points.c_str() );
272         return;
273     }
274     m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
275
276     Anchor *pAnc = new Anchor( getIntf(), rData.m_xPos, rData.m_yPos,
277                                rData.m_range, rData.m_priority,
278                                *pCurve, *pLayout );
279     pLayout->addAnchor( pAnc );
280 }
281
282
283 void Builder::addButton( const BuilderData::Button &rData )
284 {
285     // Get the bitmaps of the button
286     GenericBitmap *pBmpUp = NULL;
287     GET_BMP( pBmpUp, rData.m_upId );
288
289     GenericBitmap *pBmpDown = pBmpUp;
290     GET_BMP( pBmpDown, rData.m_downId );
291
292     GenericBitmap *pBmpOver = pBmpUp;
293     GET_BMP( pBmpOver, rData.m_overId );
294
295     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
296     if( pLayout == NULL )
297     {
298         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
299         return;
300     }
301
302     CmdGeneric *pCommand = parseAction( rData.m_actionId );
303     if( pCommand == NULL )
304     {
305         msg_Err( getIntf(), "Invalid action: %s", rData.m_actionId.c_str() );
306         return;
307     }
308
309     // Get the visibility variable
310     // XXX check when it is null
311     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
312     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
313
314     CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
315         *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
316         UString( getIntf(), rData.m_help.c_str() ), pVisible );
317
318     // Compute the position of the control
319     // XXX (we suppose all the images have the same size...)
320     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
321                                        rData.m_xPos, rData.m_yPos,
322                                        pBmpUp->getWidth(),
323                                        pBmpUp->getHeight(), *pLayout );
324
325     pLayout->addControl( pButton, pos, rData.m_layer );
326
327     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
328 }
329
330
331 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
332 {
333     // Get the bitmaps of the checkbox
334     GenericBitmap *pBmpUp1 = NULL;
335     GET_BMP( pBmpUp1, rData.m_up1Id );
336
337     GenericBitmap *pBmpDown1 = pBmpUp1;
338     GET_BMP( pBmpDown1, rData.m_down1Id );
339
340     GenericBitmap *pBmpOver1 = pBmpUp1;
341     GET_BMP( pBmpOver1, rData.m_over1Id );
342
343     GenericBitmap *pBmpUp2 = NULL;
344     GET_BMP( pBmpUp2, rData.m_up2Id );
345
346     GenericBitmap *pBmpDown2 = pBmpUp2;
347     GET_BMP( pBmpDown2, rData.m_down2Id );
348
349     GenericBitmap *pBmpOver2 = pBmpUp2;
350     GET_BMP( pBmpOver2, rData.m_over2Id );
351
352     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
353     if( pLayout == NULL )
354     {
355         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
356         return;
357     }
358
359     CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
360     if( pCommand1 == NULL )
361     {
362         msg_Err( getIntf(), "Invalid action: %s", rData.m_action1.c_str() );
363         return;
364     }
365
366     CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
367     if( pCommand2 == NULL )
368     {
369         msg_Err( getIntf(), "Invalid action: %s", rData.m_action2.c_str() );
370         return;
371     }
372
373     // Get the state variable
374     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
375     VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
376     if( pVar == NULL )
377     {
378         // TODO: default state
379         return;
380     }
381
382     // Get the visibility variable
383     // XXX check when it is null
384     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
385
386     // Create the control
387     CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
388         *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
389         *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
390         UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
391         UString( getIntf(), rData.m_help.c_str() ), pVisible );
392
393     // Compute the position of the control
394     // XXX (we suppose all the images have the same size...)
395     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
396                                        rData.m_xPos, rData.m_yPos,
397                                        pBmpUp1->getWidth(),
398                                        pBmpUp1->getHeight(), *pLayout );
399
400     pLayout->addControl( pCheckbox, pos, rData.m_layer );
401
402     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
403 }
404
405
406 void Builder::addImage( const BuilderData::Image &rData )
407 {
408     GenericBitmap *pBmp = NULL;
409     GET_BMP( pBmp, rData.m_bmpId );
410
411     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
412     if( pLayout == NULL )
413     {
414         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
415         return;
416     }
417
418     TopWindow *pWindow = m_pTheme->getWindowById(rData.m_windowId);
419     if( pWindow == NULL )
420     {
421         msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
422         return;
423     }
424
425     // Get the visibility variable
426     // XXX check when it is null
427     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
428     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
429
430     CtrlImage::resize_t resizeMethod =
431         (rData.m_resize == "scale" ? CtrlImage::kScale : CtrlImage::kMosaic);
432     CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, resizeMethod,
433         UString( getIntf(), rData.m_help.c_str() ), pVisible );
434
435     // Compute the position of the control
436     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
437                                        rData.m_xPos,
438                                        rData.m_yPos, pBmp->getWidth(),
439                                        pBmp->getHeight(), *pLayout );
440
441     // XXX: test to be changed! XXX
442     if( rData.m_actionId == "move" )
443     {
444         CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
445              *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
446              NULL);
447         pLayout->addControl( pMove, pos, rData.m_layer );
448     }
449     else if( rData.m_actionId == "resizeSE" )
450     {
451         CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
452                 UString( getIntf(), rData.m_help.c_str() ), NULL );
453         pLayout->addControl( pResize, pos, rData.m_layer );
454     }
455     else
456     {
457         pLayout->addControl( pImage, pos, rData.m_layer );
458     }
459
460     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
461 }
462
463
464 void Builder::addText( const BuilderData::Text &rData )
465 {
466     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
467     if( pLayout == NULL )
468     {
469         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
470         return;
471     }
472
473     GenericFont *pFont = getFont( rData.m_fontId );
474     if( pFont == NULL )
475     {
476         msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
477         return;
478     }
479
480     // Create a text variable
481     VarText *pVar = new VarText( getIntf() );
482     UString msg( getIntf(), rData.m_text.c_str() );
483     pVar->set( msg );
484     m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
485
486     // Get the visibility variable
487     // XXX check when it is null
488     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
489     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
490
491     CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
492         UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible );
493
494     int height = pFont->getSize();
495
496     // Compute the position of the control
497     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
498                                        rData.m_xPos, rData.m_yPos,
499                                        rData.m_width, height,
500                                        *pLayout );
501
502     pLayout->addControl( pText, pos, rData.m_layer );
503
504     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
505 }
506
507
508 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
509 {
510     // Get the bitmaps of the slider
511     GenericBitmap *pSeq = NULL;
512     GET_BMP( pSeq, rData.m_sequence );
513
514     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
515     if( pLayout == NULL )
516     {
517         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
518         return;
519     }
520
521     // Get the variable associated to the slider
522     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
523     VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
524     if( pVar == NULL )
525     {
526         msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
527         return;
528     }
529
530     // Get the visibility variable
531     // XXX check when it is null
532     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
533
534     // Create the control
535     CtrlRadialSlider *pRadial =
536         new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
537                               rData.m_minAngle, rData.m_maxAngle,
538                               UString( getIntf(), rData.m_help.c_str() ),
539                               pVisible );
540
541     // XXX: resizing is not supported
542     // Compute the position of the control
543     const Position pos =
544         makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
545                       rData.m_yPos, pSeq->getWidth(),
546                       pSeq->getHeight() / rData.m_nbImages, *pLayout );
547
548     pLayout->addControl( pRadial, pos, rData.m_layer );
549
550     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
551 }
552
553
554 void Builder::addSlider( const BuilderData::Slider &rData )
555 {
556     // Get the bitmaps of the slider
557     GenericBitmap *pBmpUp = NULL;
558     GET_BMP( pBmpUp, rData.m_upId );
559
560     GenericBitmap *pBmpDown = pBmpUp;
561     GET_BMP( pBmpDown, rData.m_downId );
562
563     GenericBitmap *pBmpOver = pBmpUp;
564     GET_BMP( pBmpOver, rData.m_overId );
565
566     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
567     if( pLayout == NULL )
568     {
569         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
570         return;
571     }
572
573     Bezier *pCurve = getPoints( rData.m_points.c_str() );
574     if( pCurve == NULL )
575     {
576         msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
577                  rData.m_points.c_str() );
578         return;
579     }
580     m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
581
582     // Get the visibility variable
583     // XXX check when it is null
584     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
585     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
586
587     // Get the variable associated to the slider
588     VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
589     if( pVar == NULL )
590     {
591         msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
592         return;
593     }
594
595     // Create the cursor and background controls
596     CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
597         *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
598         UString( getIntf(), rData.m_tooltip.c_str() ),
599         UString( getIntf(), rData.m_help.c_str() ) );
600
601     CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(), *pCursor,
602         *pCurve, *pVar, rData.m_thickness, pVisible,
603         UString( getIntf(), rData.m_help.c_str() ) );
604
605     // Compute the position of the control
606     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
607                                        rData.m_xPos, rData.m_yPos,
608                                        pCurve->getWidth(), pCurve->getHeight(),
609                                        *pLayout );
610
611     pLayout->addControl( pBackground, pos, rData.m_layer );
612     pLayout->addControl( pCursor, pos, rData.m_layer );
613
614     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
615     m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
616 }
617
618
619 void Builder::addList( const BuilderData::List &rData )
620 {
621     // Get the background bitmap, if any
622     GenericBitmap *pBgBmp = NULL;
623     GET_BMP( pBgBmp, rData.m_bgImageId );
624
625     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
626     if( pLayout == NULL )
627     {
628         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
629         return;
630     }
631
632     GenericFont *pFont = getFont( rData.m_fontId );
633     if( pFont == NULL )
634     {
635         msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
636         return;
637     }
638
639     // Get the list variable
640     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
641     VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
642     if( pVar == NULL )
643     {
644         msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
645         return;
646     }
647
648     // Get the visibility variable
649     // XXX check when it is null
650     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
651
652     // Create the list control
653     CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
654        rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
655        rData.m_bgColor2, rData.m_selColor,
656        UString( getIntf(), rData.m_help.c_str() ), pVisible );
657
658     // Compute the position of the control
659     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
660                                        rData.m_xPos, rData.m_yPos,
661                                        rData.m_width, rData.m_height,
662                                        *pLayout );
663
664     pLayout->addControl( pList, pos, rData.m_layer );
665
666     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
667 }
668
669 void Builder::addTree( const BuilderData::Tree &rData )
670 {
671     // Get the bitmaps, if any
672     GenericBitmap *pBgBmp = NULL;
673     GenericBitmap *pItemBmp = NULL;
674     GenericBitmap *pOpenBmp = NULL;
675     GenericBitmap *pClosedBmp = NULL;
676     GET_BMP( pBgBmp, rData.m_bgImageId );
677     GET_BMP( pItemBmp, rData.m_itemImageId );
678     GET_BMP( pOpenBmp, rData.m_openImageId );
679     GET_BMP( pClosedBmp, rData.m_closedImageId );
680
681     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
682     if( pLayout == NULL )
683     {
684         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
685         return;
686     }
687
688     GenericFont *pFont = getFont( rData.m_fontId );
689     if( pFont == NULL )
690     {
691         msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
692         return;
693     }
694
695     // Get the list variable
696     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
697     VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
698     if( pVar == NULL )
699     {
700         msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
701         return;
702     }
703
704     // Get the visibility variable
705     // XXX check when it is null
706     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
707
708     // Create the list control
709     CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
710        pItemBmp, pOpenBmp, pClosedBmp,
711        rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
712        rData.m_bgColor2, rData.m_selColor,
713        UString( getIntf(), rData.m_help.c_str() ), pVisible );
714
715     // Compute the position of the control
716     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
717                                        rData.m_xPos, rData.m_yPos,
718                                        rData.m_width, rData.m_height,
719                                        *pLayout );
720
721     pLayout->addControl( pTree, pos, rData.m_layer );
722
723     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
724 }
725
726 void Builder::addVideo( const BuilderData::Video &rData )
727 {
728     GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
729     if( pLayout == NULL )
730     {
731         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
732         return;
733     }
734
735     // Get the visibility variable
736     // XXX check when it is null
737     Interpreter *pInterpreter = Interpreter::instance( getIntf() );
738     VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
739
740     CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
741         rData.m_autoResize, UString( getIntf(), rData.m_help.c_str() ),
742         pVisible );
743
744     // Compute the position of the control
745     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
746                                        rData.m_xPos, rData.m_yPos,
747                                        rData.m_width, rData.m_height,
748                                        *pLayout );
749
750     pLayout->addControl( pVideo, pos, rData.m_layer );
751
752     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
753 }
754
755
756 const Position Builder::makePosition( const string &rLeftTop,
757                                       const string &rRightBottom,
758                                       int xPos, int yPos, int width,
759                                       int height, const Box &rBox ) const
760 {
761     int left = 0, top = 0, right = 0, bottom = 0;
762     Position::Ref_t refLeftTop = Position::kLeftTop;
763     Position::Ref_t refRightBottom = Position::kLeftTop;
764
765     int boxWidth = rBox.getWidth();
766     int boxHeight = rBox.getHeight();
767
768     // Position of the left top corner
769     if( rLeftTop == "lefttop" )
770     {
771         left = xPos;
772         top = yPos;
773         refLeftTop = Position::kLeftTop;
774     }
775     else if( rLeftTop == "righttop" )
776     {
777         left = xPos - boxWidth + 1;
778         top = yPos;
779         refLeftTop = Position::kRightTop;
780     }
781     else if( rLeftTop == "leftbottom" )
782     {
783         left = xPos;
784         top = yPos - boxHeight + 1;
785         refLeftTop = Position::kLeftBottom;
786     }
787     else if( rLeftTop == "rightbottom" )
788     {
789         left = xPos - boxWidth + 1;
790         top = yPos - boxHeight + 1;
791         refLeftTop = Position::kRightBottom;
792     }
793
794     // Position of the right bottom corner
795     if( rRightBottom == "lefttop" )
796     {
797         right = xPos + width - 1;
798         bottom = yPos + height - 1;
799         refRightBottom = Position::kLeftTop;
800     }
801     else if( rRightBottom == "righttop" )
802     {
803         right = xPos + width - boxWidth;
804         bottom = yPos + height - 1;
805         refRightBottom = Position::kRightTop;
806     }
807     else if( rRightBottom == "leftbottom" )
808     {
809         right = xPos + width - 1;
810         bottom = yPos + height - boxHeight;
811         refRightBottom = Position::kLeftBottom;
812     }
813     else if( rRightBottom == "rightbottom" )
814     {
815         right = xPos + width - boxWidth;
816         bottom = yPos + height - boxHeight;
817         refRightBottom = Position::kRightBottom;
818     }
819
820     return Position( left, top, right, bottom, rBox, refLeftTop,
821                      refRightBottom );
822 }
823
824
825 GenericFont *Builder::getFont( const string &fontId )
826 {
827     GenericFont *pFont = m_pTheme->getFontById(fontId);
828     if( !pFont && fontId == "defaultfont" )
829     {
830         // Get the resource path and try to load the default font
831         OSFactory *pOSFactory = OSFactory::instance( getIntf() );
832         const list<string> &resPath = pOSFactory->getResourcePath();
833         const string &sep = pOSFactory->getDirSeparator();
834
835         list<string>::const_iterator it;
836         for( it = resPath.begin(); it != resPath.end(); it++ )
837         {
838             string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
839             pFont = new FT2Font( getIntf(), path, 12 );
840             if( pFont->init() )
841             {
842                 // Font loaded successfully
843                 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
844                 break;
845             }
846             else
847             {
848                 delete pFont;
849                 pFont = NULL;
850             }
851         }
852         if( !pFont )
853         {
854             msg_Err( getIntf(), "Failed to open the default font" );
855         }
856     }
857     return pFont;
858 }
859
860
861 Bezier *Builder::getPoints( const char *pTag ) const
862 {
863     vector<float> xBez, yBez;
864     int x, y, n;
865     while( 1 )
866     {
867         if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
868         {
869             return NULL;
870         }
871 #if 0
872         if( x < 0 || y < 0 )
873         {
874             msg_Err( getIntf(),
875                      "Slider points cannot have negative coordinates!" );
876             return NULL;
877         }
878 #endif
879         xBez.push_back( x );
880         yBez.push_back( y );
881         pTag += n;
882         if( *pTag == '\0' )
883         {
884             break;
885         }
886         if( *(pTag++) != ',' )
887         {
888             return NULL;
889         }
890     }
891
892     // Create the Bezier curve
893     return new Bezier( getIntf(), xBez, yBez );
894 }
895