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