1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
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"
52 Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData):
53 SkinObject( pIntf ), m_rData( rData ), m_pTheme( NULL )
58 CmdGeneric *Builder::parseAction( const string &rAction )
60 return Interpreter::instance( getIntf() )->parseAction( rAction, m_pTheme );
65 #define ADD_OBJECTS( type ) \
66 list<BuilderData::type>::const_iterator it##type; \
67 for( it##type = m_rData.m_list##type.begin(); \
68 it##type != m_rData.m_list##type.end(); it##type++ ) \
70 add##type( *it##type ); \
74 Theme *Builder::build()
76 m_pTheme = new Theme( getIntf() );
77 if( m_pTheme == NULL )
82 // Create everything from the data in the XML
84 ADD_OBJECTS( Bitmap );
85 ADD_OBJECTS( BitmapFont );
87 ADD_OBJECTS( Window );
88 ADD_OBJECTS( Layout );
89 ADD_OBJECTS( Anchor );
90 ADD_OBJECTS( Button );
91 ADD_OBJECTS( Checkbox );
94 ADD_OBJECTS( RadialSlider );
95 ADD_OBJECTS( Slider );
103 // Macro to get a bitmap by its ID in the builder
104 #define GET_BMP( pBmp, id ) \
107 pBmp = m_pTheme->m_bitmaps[id].get(); \
110 msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
115 void Builder::addTheme( const BuilderData::Theme &rData )
117 WindowManager &rManager = m_pTheme->getWindowManager();
118 rManager.setMagnetValue( rData.m_magnet );
119 rManager.setAlphaValue( rData.m_alpha );
120 rManager.setMoveAlphaValue( rData.m_moveAlpha );
121 GenericFont *pFont = getFont( rData.m_tooltipfont );
124 rManager.createTooltip( *pFont );
128 msg_Warn( getIntf(), "Invalid tooltip font: %s",
129 rData.m_tooltipfont.c_str() );
134 void Builder::addBitmap( const BuilderData::Bitmap &rData )
136 GenericBitmap *pBmp = new PngBitmap( getIntf(), rData.m_fileName,
137 rData.m_alphaColor );
138 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
142 void Builder::addBitmapFont( const BuilderData::BitmapFont &rData )
144 GenericBitmap *pBmp = new PngBitmap( getIntf(), rData.m_file, 0 );
145 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
147 GenericFont *pFont = new BitmapFont( getIntf(), *pBmp );
150 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
159 void Builder::addFont( const BuilderData::Font &rData )
161 GenericFont *pFont = new FT2Font( getIntf(), rData.m_fontFile,
165 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
174 void Builder::addWindow( const BuilderData::Window &rData )
177 new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
178 m_pTheme->getWindowManager(),
179 rData.m_dragDrop, rData.m_playOnDrop );
181 m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
185 void Builder::addLayout( const BuilderData::Layout &rData )
187 TopWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
190 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
194 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
195 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
196 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
198 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
200 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
202 minWidth, maxWidth, minHeight,
204 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
206 // Attach the layout to its window
207 pWin->setActiveLayout( pLayout );
211 void Builder::addAnchor( const BuilderData::Anchor &rData )
213 TopWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
216 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
220 Bezier *pCurve = getPoints( rData.m_points.c_str() );
223 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
224 rData.m_points.c_str() );
227 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
229 Anchor *pAnc = new Anchor( getIntf(), rData.m_xPos, rData.m_yPos,
230 rData.m_range, rData.m_priority,
232 pWin->addAnchor( pAnc );
236 void Builder::addButton( const BuilderData::Button &rData )
238 // Get the bitmaps of the button
239 GenericBitmap *pBmpUp = NULL;
240 GET_BMP( pBmpUp, rData.m_upId );
242 GenericBitmap *pBmpDown = pBmpUp;
243 GET_BMP( pBmpDown, rData.m_downId );
245 GenericBitmap *pBmpOver = pBmpUp;
246 GET_BMP( pBmpOver, rData.m_overId );
248 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
249 if( pLayout == NULL )
251 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
255 CmdGeneric *pCommand = parseAction( rData.m_actionId );
256 if( pCommand == NULL )
258 msg_Err( getIntf(), "Invalid action: %s", rData.m_actionId.c_str() );
262 // Get the visibility variable
263 // XXX check when it is null
264 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
265 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
267 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
268 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
269 UString( getIntf(), rData.m_help.c_str() ), pVisible );
271 // Compute the position of the control
272 // XXX (we suppose all the images have the same size...)
273 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
274 rData.m_xPos, rData.m_yPos,
276 pBmpUp->getHeight(), *pLayout );
278 pLayout->addControl( pButton, pos, rData.m_layer );
280 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
284 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
286 // Get the bitmaps of the checkbox
287 GenericBitmap *pBmpUp1 = NULL;
288 GET_BMP( pBmpUp1, rData.m_up1Id );
290 GenericBitmap *pBmpDown1 = pBmpUp1;
291 GET_BMP( pBmpDown1, rData.m_down1Id );
293 GenericBitmap *pBmpOver1 = pBmpUp1;
294 GET_BMP( pBmpOver1, rData.m_over1Id );
296 GenericBitmap *pBmpUp2 = NULL;
297 GET_BMP( pBmpUp2, rData.m_up2Id );
299 GenericBitmap *pBmpDown2 = pBmpUp2;
300 GET_BMP( pBmpDown2, rData.m_down2Id );
302 GenericBitmap *pBmpOver2 = pBmpUp2;
303 GET_BMP( pBmpOver2, rData.m_over2Id );
305 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
306 if( pLayout == NULL )
308 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
312 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
313 if( pCommand1 == NULL )
315 msg_Err( getIntf(), "Invalid action: %s", rData.m_action1.c_str() );
319 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
320 if( pCommand2 == NULL )
322 msg_Err( getIntf(), "Invalid action: %s", rData.m_action2.c_str() );
326 // Get the state variable
327 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
328 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
331 // TODO: default state
335 // Get the visibility variable
336 // XXX check when it is null
337 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
339 // Create the control
340 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
341 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
342 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
343 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
344 UString( getIntf(), rData.m_help.c_str() ), pVisible );
346 // Compute the position of the control
347 // XXX (we suppose all the images have the same size...)
348 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
349 rData.m_xPos, rData.m_yPos,
351 pBmpUp1->getHeight(), *pLayout );
353 pLayout->addControl( pCheckbox, pos, rData.m_layer );
355 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
359 void Builder::addImage( const BuilderData::Image &rData )
361 GenericBitmap *pBmp = NULL;
362 GET_BMP( pBmp, rData.m_bmpId );
364 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
365 if( pLayout == NULL )
367 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
371 TopWindow *pWindow = m_pTheme->m_windows[rData.m_windowId].get();
372 if( pWindow == NULL )
374 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
378 // Get the visibility variable
379 // XXX check when it is null
380 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
381 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
383 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp,
384 UString( getIntf(), rData.m_help.c_str() ), pVisible );
386 // Compute the position of the control
387 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
389 rData.m_yPos, pBmp->getWidth(),
390 pBmp->getHeight(), *pLayout );
392 // XXX: test to be changed! XXX
393 if( rData.m_actionId == "move" )
395 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
396 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
398 pLayout->addControl( pMove, pos, rData.m_layer );
400 else if( rData.m_actionId == "resize" )
402 CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
403 UString( getIntf(), rData.m_help.c_str() ), NULL );
404 pLayout->addControl( pResize, pos, rData.m_layer );
408 pLayout->addControl( pImage, pos, rData.m_layer );
411 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
415 void Builder::addText( const BuilderData::Text &rData )
417 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
418 if( pLayout == NULL )
420 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
424 GenericFont *pFont = getFont( rData.m_fontId );
427 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
431 // Create a text variable
432 VarText *pVar = new VarText( getIntf() );
433 UString msg( getIntf(), rData.m_text.c_str() );
435 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
437 // Get the visibility variable
438 // XXX check when it is null
439 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
440 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
442 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
443 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible );
445 int height = pFont->getSize();
447 pLayout->addControl( pText, Position( rData.m_xPos, rData.m_yPos,
448 rData.m_xPos + rData.m_width,
449 rData.m_yPos + height, *pLayout ),
452 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
456 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
458 // Get the bitmaps of the slider
459 GenericBitmap *pSeq = NULL;
460 GET_BMP( pSeq, rData.m_sequence );
462 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
463 if( pLayout == NULL )
465 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
469 // Get the variable associated to the slider
470 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
471 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
474 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
478 // Get the visibility variable
479 // XXX check when it is null
480 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
482 // Create the control
483 CtrlRadialSlider *pRadial =
484 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
485 rData.m_minAngle, rData.m_maxAngle,
486 UString( getIntf(), rData.m_help.c_str() ),
489 // XXX: resizing is not supported
490 // Compute the position of the control
492 makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
493 rData.m_yPos, pSeq->getWidth(),
494 pSeq->getHeight() / rData.m_nbImages, *pLayout );
496 pLayout->addControl( pRadial, pos, rData.m_layer );
498 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
502 void Builder::addSlider( const BuilderData::Slider &rData )
504 // Get the bitmaps of the slider
505 GenericBitmap *pBmpUp = NULL;
506 GET_BMP( pBmpUp, rData.m_upId );
508 GenericBitmap *pBmpDown = pBmpUp;
509 GET_BMP( pBmpDown, rData.m_downId );
511 GenericBitmap *pBmpOver = pBmpUp;
512 GET_BMP( pBmpOver, rData.m_overId );
514 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
515 if( pLayout == NULL )
517 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
521 Bezier *pCurve = getPoints( rData.m_points.c_str() );
524 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
525 rData.m_points.c_str() );
528 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
530 // Get the visibility variable
531 // XXX check when it is null
532 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
533 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
535 // Get the variable associated to the slider
536 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
539 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
543 // Create the cursor and background controls
544 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
545 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
546 UString( getIntf(), rData.m_tooltip.c_str() ),
547 UString( getIntf(), rData.m_help.c_str() ) );
549 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(), *pCursor,
550 *pCurve, *pVar, rData.m_thickness, pVisible,
551 UString( getIntf(), rData.m_help.c_str() ) );
553 // Compute the position of the control
554 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
555 rData.m_xPos, rData.m_yPos,
556 pCurve->getWidth(), pCurve->getHeight(),
559 pLayout->addControl( pBackground, pos, rData.m_layer );
560 pLayout->addControl( pCursor, pos, rData.m_layer );
562 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
563 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
567 void Builder::addList( const BuilderData::List &rData )
569 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
570 if( pLayout == NULL )
572 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
576 GenericFont *pFont = getFont( rData.m_fontId );
579 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
583 // Get the list variable
584 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
585 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
588 msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
592 // Get the visibility variable
593 // XXX check when it is null
594 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
596 // Create the list control
597 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont,
598 rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
599 rData.m_bgColor2, rData.m_selColor,
600 UString( getIntf(), rData.m_help.c_str() ), pVisible );
602 // Compute the position of the control
603 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
604 rData.m_xPos, rData.m_yPos,
605 rData.m_width, rData.m_height,
608 pLayout->addControl( pList, pos, rData.m_layer );
610 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
614 void Builder::addVideo( const BuilderData::Video &rData )
616 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
617 if( pLayout == NULL )
619 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
623 // Get the visibility variable
624 // XXX check when it is null
625 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
626 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
628 CtrlVideo *pVideo = new CtrlVideo( getIntf(),
629 UString( getIntf(), rData.m_help.c_str() ), pVisible );
631 // Compute the position of the control
632 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
633 rData.m_xPos, rData.m_yPos,
634 rData.m_width, rData.m_height,
637 pLayout->addControl( pVideo, pos, rData.m_layer );
639 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
643 const Position Builder::makePosition( const string &rLeftTop,
644 const string &rRightBottom,
645 int xPos, int yPos, int width,
646 int height, const Box &rBox ) const
648 int left = 0, top = 0, right = 0, bottom = 0;
649 Position::Ref_t refLeftTop = Position::kLeftTop;
650 Position::Ref_t refRightBottom = Position::kLeftTop;
652 int boxWidth = rBox.getWidth();
653 int boxHeight = rBox.getHeight();
655 // Position of the left top corner
656 if( rLeftTop == "lefttop" )
660 refLeftTop = Position::kLeftTop;
662 else if( rLeftTop == "righttop" )
664 left = xPos - boxWidth + 1;
666 refLeftTop = Position::kRightTop;
668 else if( rLeftTop == "leftbottom" )
671 top = yPos - boxHeight + 1;
672 refLeftTop = Position::kLeftBottom;
674 else if( rLeftTop == "rightbottom" )
676 left = xPos - boxWidth + 1;
677 top = yPos - boxHeight + 1;
678 refLeftTop = Position::kRightBottom;
681 // Position of the right bottom corner
682 if( rRightBottom == "lefttop" )
684 right = xPos + width - 1;
685 bottom = yPos + height - 1;
686 refRightBottom = Position::kLeftTop;
688 else if( rRightBottom == "righttop" )
690 right = xPos + width - boxWidth;
691 bottom = yPos + height - 1;
692 refRightBottom = Position::kRightTop;
694 else if( rRightBottom == "leftbottom" )
696 right = xPos + width - 1;
697 bottom = yPos + height - boxHeight;
698 refRightBottom = Position::kLeftBottom;
700 else if( rRightBottom == "rightbottom" )
702 right = xPos + width - boxWidth;
703 bottom = yPos + height - boxHeight;
704 refRightBottom = Position::kRightBottom;
707 return Position( left, top, right, bottom, rBox, refLeftTop,
712 GenericFont *Builder::getFont( const string &fontId )
714 GenericFont *pFont = m_pTheme->m_fonts[fontId].get();
715 if( !pFont && fontId == "defaultfont" )
718 string defaultFont = (string)getIntf()->p_libvlc->psz_vlcpath +
719 "\\skins2\\fonts\\FreeSans.ttf";
721 string defaultFont = (string)DATA_PATH + "/skins2/fonts/FreeSans.ttf";
723 pFont = new FT2Font( getIntf(), defaultFont, 12 );
726 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
738 Bezier *Builder::getPoints( const char *pTag ) const
740 vector<float> xBez, yBez;
744 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
752 "Slider points cannot have negative coordinates!" );
763 if( *(pTag++) != ',' )
769 // Create the Bezier curve
770 return new Bezier( getIntf(), xBez, yBez );