1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "builder.hpp"
26 #include "builder_data.hpp"
27 #include "interpreter.hpp"
28 #include "skin_parser.hpp"
29 #include "../src/file_bitmap.hpp"
30 #include "../src/os_factory.hpp"
31 #include "../src/generic_bitmap.hpp"
32 #include "../src/top_window.hpp"
33 #include "../src/anchor.hpp"
34 #include "../src/bitmap_font.hpp"
35 #include "../src/ft2_font.hpp"
36 #include "../src/ini_file.hpp"
37 #include "../src/generic_layout.hpp"
38 #include "../src/popup.hpp"
39 #include "../src/theme.hpp"
40 #include "../src/window_manager.hpp"
41 #include "../src/vout_manager.hpp"
42 #include "../commands/cmd_generic.hpp"
43 #include "../controls/ctrl_button.hpp"
44 #include "../controls/ctrl_checkbox.hpp"
45 #include "../controls/ctrl_image.hpp"
46 #include "../controls/ctrl_list.hpp"
47 #include "../controls/ctrl_move.hpp"
48 #include "../controls/ctrl_resize.hpp"
49 #include "../controls/ctrl_slider.hpp"
50 #include "../controls/ctrl_radialslider.hpp"
51 #include "../controls/ctrl_text.hpp"
52 #include "../controls/ctrl_tree.hpp"
53 #include "../controls/ctrl_video.hpp"
54 #include "../utils/bezier.hpp"
55 #include "../utils/position.hpp"
56 #include "../utils/var_bool.hpp"
57 #include "../utils/var_text.hpp"
59 #include <vlc_image.h>
62 Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData,
63 const string &rPath ):
64 SkinObject( pIntf ), m_rData( rData ), m_path( rPath ), m_pTheme( NULL )
66 m_pImageHandler = image_HandlerCreate( pIntf );
71 if( m_pImageHandler ) image_HandlerDelete( m_pImageHandler );
74 CmdGeneric *Builder::parseAction( const string &rAction )
76 return Interpreter::instance( getIntf() )->parseAction( rAction, m_pTheme );
79 template<class T> inline
80 void Builder::add_objects(const std::list<T> &list,
81 void (Builder::*addfn)(const T &))
83 typename std::list<T>::const_iterator i;
84 for( i = list.begin(); i != list.end(); ++i )
88 Theme *Builder::build()
90 #define ADD_OBJECTS( type ) \
91 add_objects(m_rData.m_list##type,&Builder::add##type)
93 m_pTheme = new (std::nothrow) Theme( getIntf() );
94 if( m_pTheme == NULL )
97 // Create everything from the data in the XML
99 ADD_OBJECTS( IniFile );
100 ADD_OBJECTS( Bitmap );
101 ADD_OBJECTS( SubBitmap );
102 ADD_OBJECTS( BitmapFont );
104 ADD_OBJECTS( Window );
105 // XXX: PopupMenus are created after the windows, so that the Win32Factory
106 // (at least) can give a valid window handle to the OSPopup objects
107 ADD_OBJECTS( PopupMenu );
108 ADD_OBJECTS( Layout );
109 ADD_OBJECTS( Panel );
110 ADD_OBJECTS( Anchor );
111 ADD_OBJECTS( Button );
112 ADD_OBJECTS( Checkbox );
113 ADD_OBJECTS( Image );
115 ADD_OBJECTS( RadialSlider );
116 ADD_OBJECTS( Slider );
119 ADD_OBJECTS( Video );
120 // MenuItems must be created after all the rest, so that the IDs of the
121 // other elements exist and can be parsed in the actions
122 ADD_OBJECTS( MenuItem );
123 ADD_OBJECTS( MenuSeparator );
131 // Macro to get a bitmap by its ID in the builder
132 #define GET_BMP( pBmp, id ) \
135 pBmp = m_pTheme->getBitmapById(id); \
138 msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
144 // Macro to get the parent box of a control, given the panel ID
145 #define GET_BOX( pRect, id, pLayout ) \
147 pRect = &pLayout->getRect(); \
150 const Position *pParent = \
151 m_pTheme->getPositionById( rData.m_panelId ); \
152 if( pParent == NULL ) \
154 msg_Err( getIntf(), "parent panel could not be found: %s", \
155 rData.m_panelId.c_str() ); \
162 void Builder::addTheme( const BuilderData::Theme &rData )
164 WindowManager &rManager = m_pTheme->getWindowManager();
165 rManager.setMagnetValue( rData.m_magnet );
166 rManager.setAlphaValue( rData.m_alpha );
167 rManager.setMoveAlphaValue( rData.m_moveAlpha );
168 GenericFont *pFont = getFont( rData.m_tooltipfont );
171 rManager.createTooltip( *pFont );
175 msg_Warn( getIntf(), "invalid tooltip font: %s",
176 rData.m_tooltipfont.c_str() );
181 void Builder::addIniFile( const BuilderData::IniFile &rData )
183 // Parse the INI file
184 IniFile iniFile( getIntf(), rData.m_id, getFilePath( rData.m_file ) );
189 void Builder::addBitmap( const BuilderData::Bitmap &rData )
191 GenericBitmap *pBmp =
192 new FileBitmap( getIntf(), m_pImageHandler,
193 getFilePath( rData.m_fileName ), rData.m_alphaColor,
194 rData.m_nbFrames, rData.m_fps, rData.m_nbLoops );
195 if( !pBmp->getData() )
201 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
205 void Builder::addSubBitmap( const BuilderData::SubBitmap &rData )
207 if( m_pTheme->m_bitmaps.find( rData.m_id ) != m_pTheme->m_bitmaps.end() )
209 msg_Dbg( getIntf(), "bitmap %s already exists", rData.m_id.c_str() );
213 // Get the parent bitmap
214 GenericBitmap *pParentBmp = NULL;
215 GET_BMP( pParentBmp, rData.m_parent );
217 // Copy a region of the parent bitmap to the new one
219 new BitmapImpl( getIntf(), rData.m_width, rData.m_height,
220 rData.m_nbFrames, rData.m_fps, rData.m_nbLoops );
221 bool res = pBmp->drawBitmap( *pParentBmp, rData.m_x, rData.m_y, 0, 0,
222 rData.m_width, rData.m_height );
225 // Invalid sub-bitmap
227 msg_Warn( getIntf(), "sub-bitmap %s ignored", rData.m_id.c_str() );
230 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
234 void Builder::addBitmapFont( const BuilderData::BitmapFont &rData )
236 if( m_pTheme->m_fonts.find( rData.m_id ) != m_pTheme->m_fonts.end() )
238 msg_Dbg( getIntf(), "font %s already exists", rData.m_id.c_str() );
242 GenericBitmap *pBmp =
243 new FileBitmap( getIntf(), m_pImageHandler,
244 getFilePath( rData.m_file ), 0 );
245 if( !pBmp->getData() )
252 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
254 GenericFont *pFont = new BitmapFont( getIntf(), *pBmp, rData.m_type );
257 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
266 void Builder::addFont( const BuilderData::Font &rData )
268 // Try to load the font from the theme directory
269 GenericFont *pFont = new FT2Font( getIntf(),
270 getFilePath( rData.m_fontFile ),
274 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
280 // Font not found; try in the resource path
281 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
282 const list<string> &resPath = pOSFactory->getResourcePath();
283 const string &sep = pOSFactory->getDirSeparator();
285 list<string>::const_iterator it;
286 for( it = resPath.begin(); it != resPath.end(); it++ )
288 string path = (*it) + sep + "fonts" + sep + rData.m_fontFile;
289 pFont = new FT2Font( getIntf(), path, rData.m_size );
292 // Font loaded successfully
293 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
305 void Builder::addPopupMenu( const BuilderData::PopupMenu &rData )
307 Popup *pPopup = new Popup( getIntf(), m_pTheme->getWindowManager() );
309 m_pTheme->m_popups[rData.m_id] = PopupPtr( pPopup );
313 void Builder::addMenuItem( const BuilderData::MenuItem &rData )
315 Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
318 msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
322 CmdGeneric *pCommand = parseAction( rData.m_action );
323 if( pCommand == NULL )
325 msg_Err( getIntf(), "invalid action: %s", rData.m_action.c_str() );
329 pPopup->addItem( rData.m_label, *pCommand, rData.m_pos );
333 void Builder::addMenuSeparator( const BuilderData::MenuSeparator &rData )
335 Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
338 msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
342 pPopup->addSeparator( rData.m_pos );
346 void Builder::addWindow( const BuilderData::Window &rData )
349 new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
350 m_pTheme->getWindowManager(),
351 rData.m_dragDrop, rData.m_playOnDrop,
354 m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
356 if( rData.m_id == "fullscreenController" )
357 VoutManager::instance( getIntf())->registerFSC( pWin );
361 void Builder::addLayout( const BuilderData::Layout &rData )
363 TopWindow *pWin = m_pTheme->getWindowById( rData.m_windowId );
366 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
370 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
371 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
372 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
374 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
376 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
378 minWidth, maxWidth, minHeight,
380 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
382 // Attach the layout to its window
383 m_pTheme->getWindowManager().addLayout( *pWin, *pLayout );
387 void Builder::addAnchor( const BuilderData::Anchor &rData )
389 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
390 if( pLayout == NULL )
392 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
396 Bezier *pCurve = getPoints( rData.m_points.c_str() );
399 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
400 rData.m_points.c_str() );
403 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
405 // Compute the position of the anchor
406 const Position pos = makePosition( rData.m_leftTop, rData.m_leftTop,
407 rData.m_xPos, rData.m_yPos,
410 pLayout->getRect() );
412 Anchor *pAnc = new Anchor( getIntf(), pos, rData.m_range, rData.m_priority,
414 pLayout->addAnchor( pAnc );
418 void Builder::addButton( const BuilderData::Button &rData )
420 // Get the bitmaps of the button
421 GenericBitmap *pBmpUp = NULL;
422 GET_BMP( pBmpUp, rData.m_upId );
424 GenericBitmap *pBmpDown = pBmpUp;
425 GET_BMP( pBmpDown, rData.m_downId );
427 GenericBitmap *pBmpOver = pBmpUp;
428 GET_BMP( pBmpOver, rData.m_overId );
430 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
431 if( pLayout == NULL )
433 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
437 CmdGeneric *pCommand = parseAction( rData.m_actionId );
438 if( pCommand == NULL )
440 msg_Err( getIntf(), "invalid action: %s", rData.m_actionId.c_str() );
444 // Get the visibility variable
445 // XXX check when it is null
446 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
447 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
449 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
450 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
451 UString( getIntf(), rData.m_help.c_str() ), pVisible );
452 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
454 // Compute the position of the control
455 // XXX (we suppose all the images have the same size...)
456 const GenericRect *pRect;
457 GET_BOX( pRect, rData.m_panelId , pLayout);
458 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
459 rData.m_xPos, rData.m_yPos,
461 pBmpUp->getHeight(), *pRect,
462 rData.m_xKeepRatio, rData.m_yKeepRatio );
464 pLayout->addControl( pButton, pos, rData.m_layer );
468 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
470 // Get the bitmaps of the checkbox
471 GenericBitmap *pBmpUp1 = NULL;
472 GET_BMP( pBmpUp1, rData.m_up1Id );
474 GenericBitmap *pBmpDown1 = pBmpUp1;
475 GET_BMP( pBmpDown1, rData.m_down1Id );
477 GenericBitmap *pBmpOver1 = pBmpUp1;
478 GET_BMP( pBmpOver1, rData.m_over1Id );
480 GenericBitmap *pBmpUp2 = NULL;
481 GET_BMP( pBmpUp2, rData.m_up2Id );
483 GenericBitmap *pBmpDown2 = pBmpUp2;
484 GET_BMP( pBmpDown2, rData.m_down2Id );
486 GenericBitmap *pBmpOver2 = pBmpUp2;
487 GET_BMP( pBmpOver2, rData.m_over2Id );
489 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
490 if( pLayout == NULL )
492 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
496 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
497 if( pCommand1 == NULL )
499 msg_Err( getIntf(), "invalid action: %s", rData.m_action1.c_str() );
503 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
504 if( pCommand2 == NULL )
506 msg_Err( getIntf(), "invalid action: %s", rData.m_action2.c_str() );
510 // Get the state variable
511 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
512 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
515 // TODO: default state
519 // Get the visibility variable
520 // XXX check when it is null
521 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
523 // Create the control
524 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
525 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
526 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
527 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
528 UString( getIntf(), rData.m_help.c_str() ), pVisible );
529 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
531 // Compute the position of the control
532 // XXX (we suppose all the images have the same size...)
533 const GenericRect *pRect;
534 GET_BOX( pRect, rData.m_panelId , pLayout);
535 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
536 rData.m_xPos, rData.m_yPos,
538 pBmpUp1->getHeight(), *pRect,
539 rData.m_xKeepRatio, rData.m_yKeepRatio );
541 pLayout->addControl( pCheckbox, pos, rData.m_layer );
545 void Builder::addImage( const BuilderData::Image &rData )
547 GenericBitmap *pBmp = NULL;
548 GET_BMP( pBmp, rData.m_bmpId );
550 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
551 if( pLayout == NULL )
553 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
557 TopWindow *pWindow = m_pTheme->getWindowById( rData.m_windowId );
558 if( pWindow == NULL )
560 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
564 CmdGeneric *pCommand = parseAction( rData.m_action2Id );
565 if( pCommand == NULL )
567 msg_Err( getIntf(), "invalid action: %s", rData.m_action2Id.c_str() );
571 // Get the visibility variable
572 // XXX check when it is null
573 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
574 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
576 CtrlImage::resize_t resizeMethod =
577 (rData.m_resize == "scale" ? CtrlImage::kScale : CtrlImage::kMosaic);
578 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, *pCommand,
579 resizeMethod, UString( getIntf(), rData.m_help.c_str() ), pVisible );
580 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
582 // Compute the position of the control
583 const GenericRect *pRect;
584 GET_BOX( pRect, rData.m_panelId , pLayout);
585 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
586 rData.m_xPos, rData.m_yPos,
587 pBmp->getWidth(), pBmp->getHeight(),
588 *pRect, rData.m_xKeepRatio,
589 rData.m_yKeepRatio );
591 if( rData.m_actionId == "move" )
593 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
594 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
596 m_pTheme->m_controls[rData.m_id + "_move"] = CtrlGenericPtr( pMove );
597 pLayout->addControl( pMove, pos, rData.m_layer );
599 else if( rData.m_actionId == "resizeS" )
601 CtrlResize *pResize = new CtrlResize( getIntf(),
602 m_pTheme->getWindowManager(), *pImage, *pLayout,
603 UString( getIntf(), rData.m_help.c_str() ), pVisible,
604 WindowManager::kResizeS );
605 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
606 pLayout->addControl( pResize, pos, rData.m_layer );
608 else if( rData.m_actionId == "resizeE" )
610 CtrlResize *pResize = new CtrlResize( getIntf(),
611 m_pTheme->getWindowManager(), *pImage, *pLayout,
612 UString( getIntf(), rData.m_help.c_str() ), pVisible,
613 WindowManager::kResizeE );
614 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
615 pLayout->addControl( pResize, pos, rData.m_layer );
617 else if( rData.m_actionId == "resizeSE" )
619 CtrlResize *pResize = new CtrlResize( getIntf(),
620 m_pTheme->getWindowManager(), *pImage, *pLayout,
621 UString( getIntf(), rData.m_help.c_str() ), pVisible,
622 WindowManager::kResizeSE );
623 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
624 pLayout->addControl( pResize, pos, rData.m_layer );
628 pLayout->addControl( pImage, pos, rData.m_layer );
633 void Builder::addPanel( const BuilderData::Panel &rData )
635 // This method makes the assumption that the Panels are created in the
636 // order of the XML, because each child Panel expects its parent Panel
637 // in order to be fully created
639 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
640 if( pLayout == NULL )
642 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
646 const GenericRect *pRect;
647 GET_BOX( pRect, rData.m_panelId , pLayout);
649 new Position( makePosition( rData.m_leftTop, rData.m_rightBottom,
650 rData.m_xPos, rData.m_yPos,
651 rData.m_width, rData.m_height,
652 *pRect, rData.m_xKeepRatio,
653 rData.m_yKeepRatio ) );
654 m_pTheme->m_positions[rData.m_id] = PositionPtr( pPos );
658 void Builder::addText( const BuilderData::Text &rData )
660 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
661 if( pLayout == NULL )
663 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
667 GenericFont *pFont = getFont( rData.m_fontId );
670 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
674 // Convert the scrolling mode
675 CtrlText::Scrolling_t scrolling;
676 if( rData.m_scrolling == "auto" )
677 scrolling = CtrlText::kAutomatic;
678 else if( rData.m_scrolling == "manual" )
679 scrolling = CtrlText::kManual;
680 else if( rData.m_scrolling == "none" )
681 scrolling = CtrlText::kNone;
684 msg_Err( getIntf(), "invalid scrolling mode: %s",
685 rData.m_scrolling.c_str() );
689 // Convert the alignment
690 CtrlText::Align_t alignment;
691 if( rData.m_alignment == "left" )
692 alignment = CtrlText::kLeft;
693 else if( rData.m_alignment == "center" || rData.m_alignment == "centre" )
694 alignment = CtrlText::kCenter;
695 else if( rData.m_alignment == "right" )
696 alignment = CtrlText::kRight;
699 msg_Err( getIntf(), "invalid alignment: %s",
700 rData.m_alignment.c_str() );
704 // Create a text variable
705 VarText *pVar = new VarText( getIntf() );
706 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
708 // Get the visibility variable
709 // XXX check when it is null
710 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
711 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
713 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
714 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible,
715 scrolling, alignment );
716 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
718 int height = pFont->getSize();
720 // Compute the position of the control
721 const GenericRect *pRect;
722 GET_BOX( pRect, rData.m_panelId , pLayout);
723 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
724 rData.m_xPos, rData.m_yPos,
725 rData.m_width, height, *pRect,
726 rData.m_xKeepRatio, rData.m_yKeepRatio );
728 pLayout->addControl( pText, pos, rData.m_layer );
730 // Set the text of the control
731 UString msg( getIntf(), rData.m_text.c_str() );
736 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
738 // Get the bitmaps of the slider
739 GenericBitmap *pSeq = NULL;
740 GET_BMP( pSeq, rData.m_sequence );
742 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
743 if( pLayout == NULL )
745 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
749 // Get the variable associated to the slider
750 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
751 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
754 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
758 // Get the visibility variable
759 // XXX check when it is null
760 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
762 // Create the control
763 CtrlRadialSlider *pRadial =
764 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
765 rData.m_minAngle, rData.m_maxAngle,
766 UString( getIntf(), rData.m_help.c_str() ),
768 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
770 // XXX: resizing is not supported
771 // Compute the position of the control
772 const GenericRect *pRect;
773 GET_BOX( pRect, rData.m_panelId , pLayout);
774 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
775 rData.m_xPos, rData.m_yPos,
777 pSeq->getHeight() / rData.m_nbImages,
779 rData.m_xKeepRatio, rData.m_yKeepRatio );
781 pLayout->addControl( pRadial, pos, rData.m_layer );
785 void Builder::addSlider( const BuilderData::Slider &rData )
787 // Add the background first, so that we will still have something almost
788 // functional if the cursor cannot be created properly (this happens for
789 // some winamp2 skins, where the images of the cursor are not always
792 // Get the bitmaps of the background
793 GenericBitmap *pBgImage = NULL;
794 if( rData.m_imageId != "none" )
796 GET_BMP( pBgImage, rData.m_imageId );
799 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
800 if( pLayout == NULL )
802 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
806 Bezier *pCurve = getPoints( rData.m_points.c_str() );
809 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
810 rData.m_points.c_str() );
813 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
815 // Get the visibility variable
816 // XXX check when it is null
817 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
818 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
820 // Get the variable associated to the slider
821 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
824 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
828 // Create the background control
829 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(),
830 *pCurve, *pVar, rData.m_thickness, pBgImage, rData.m_nbHoriz,
831 rData.m_nbVert, rData.m_padHoriz, rData.m_padVert,
832 pVisible, UString( getIntf(), rData.m_help.c_str() ) );
833 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
835 // Compute the position of the control
836 const GenericRect *pRect;
837 GET_BOX( pRect, rData.m_panelId , pLayout);
838 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
839 rData.m_xPos, rData.m_yPos,
840 pCurve->getWidth(), pCurve->getHeight(),
842 rData.m_xKeepRatio, rData.m_yKeepRatio );
844 pLayout->addControl( pBackground, pos, rData.m_layer );
846 // Get the bitmaps of the cursor
847 GenericBitmap *pBmpUp = NULL;
848 GET_BMP( pBmpUp, rData.m_upId );
850 GenericBitmap *pBmpDown = pBmpUp;
851 GET_BMP( pBmpDown, rData.m_downId );
853 GenericBitmap *pBmpOver = pBmpUp;
854 GET_BMP( pBmpOver, rData.m_overId );
856 // Create the cursor control
857 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
858 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
859 UString( getIntf(), rData.m_tooltip.c_str() ),
860 UString( getIntf(), rData.m_help.c_str() ) );
861 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
863 pLayout->addControl( pCursor, pos, rData.m_layer );
865 // Associate the cursor to the background
866 pBackground->associateCursor( *pCursor );
870 void Builder::addList( const BuilderData::List &rData )
872 // Get the background bitmap, if any
873 GenericBitmap *pBgBmp = NULL;
874 GET_BMP( pBgBmp, rData.m_bgImageId );
876 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
877 if( pLayout == NULL )
879 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
883 GenericFont *pFont = getFont( rData.m_fontId );
886 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
890 // Get the list variable
891 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
892 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
895 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
899 // Get the visibility variable
900 // XXX check when it is null
901 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
903 // Get the color values
904 uint32_t fgColor = getColor( rData.m_fgColor );
905 uint32_t playColor = getColor( rData.m_playColor );
906 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
907 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
908 uint32_t selColor = getColor( rData.m_selColor );
910 // Create the list control
911 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
912 fgColor, playColor, bgColor1, bgColor2, selColor,
913 UString( getIntf(), rData.m_help.c_str() ), pVisible );
914 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
916 // Compute the position of the control
917 const GenericRect *pRect;
918 GET_BOX( pRect, rData.m_panelId , pLayout);
919 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
920 rData.m_xPos, rData.m_yPos,
921 rData.m_width, rData.m_height,
923 rData.m_xKeepRatio, rData.m_yKeepRatio );
925 pLayout->addControl( pList, pos, rData.m_layer );
928 void Builder::addTree( const BuilderData::Tree &rData )
930 // Get the bitmaps, if any
931 GenericBitmap *pBgBmp = NULL;
932 GenericBitmap *pItemBmp = NULL;
933 GenericBitmap *pOpenBmp = NULL;
934 GenericBitmap *pClosedBmp = NULL;
935 GET_BMP( pBgBmp, rData.m_bgImageId );
936 GET_BMP( pItemBmp, rData.m_itemImageId );
937 GET_BMP( pOpenBmp, rData.m_openImageId );
938 GET_BMP( pClosedBmp, rData.m_closedImageId );
940 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
941 if( pLayout == NULL )
943 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
947 GenericFont *pFont = getFont( rData.m_fontId );
950 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
954 // Get the list variable
955 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
956 VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
959 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
963 // Get the visibility variable
964 // XXX check when it is null
965 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
966 VarBool *pFlat = pInterpreter->getVarBool( rData.m_flat, m_pTheme );
968 // Get the color values
969 uint32_t fgColor = getColor( rData.m_fgColor );
970 uint32_t playColor = getColor( rData.m_playColor );
971 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
972 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
973 uint32_t selColor = getColor( rData.m_selColor );
975 // Create the list control
976 CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
977 pItemBmp, pOpenBmp, pClosedBmp,
978 fgColor, playColor, bgColor1, bgColor2, selColor,
979 UString( getIntf(), rData.m_help.c_str() ), pVisible, pFlat );
980 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
982 // Compute the position of the control
983 const GenericRect *pRect;
984 GET_BOX( pRect, rData.m_panelId , pLayout);
985 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
986 rData.m_xPos, rData.m_yPos,
987 rData.m_width, rData.m_height,
989 rData.m_xKeepRatio, rData.m_yKeepRatio );
991 pLayout->addControl( pTree, pos, rData.m_layer );
994 void Builder::addVideo( const BuilderData::Video &rData )
996 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
997 if( pLayout == NULL )
999 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
1003 // Get the visibility variable
1004 // XXX check when it is null
1005 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1006 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
1008 CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
1009 rData.m_autoResize, UString( getIntf(), rData.m_help.c_str() ),
1011 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
1013 // if autoresize is true, force the control to resize
1014 BuilderData::Video Data = rData;
1015 if( rData.m_autoResize )
1017 Data.m_leftTop = "lefttop";
1018 Data.m_rightBottom = "rightbottom";
1021 // Compute the position of the control
1022 const GenericRect *pRect;
1023 GET_BOX( pRect, rData.m_panelId , pLayout);
1024 const Position pos = makePosition( Data.m_leftTop, Data.m_rightBottom,
1025 Data.m_xPos, Data.m_yPos,
1026 Data.m_width, Data.m_height,
1028 Data.m_xKeepRatio, Data.m_yKeepRatio );
1030 pLayout->addControl( pVideo, pos, rData.m_layer );
1034 const Position Builder::makePosition( const string &rLeftTop,
1035 const string &rRightBottom,
1036 int xPos, int yPos, int width,
1037 int height, const GenericRect &rRect,
1038 bool xKeepRatio, bool yKeepRatio ) const
1040 int left = 0, top = 0, right = 0, bottom = 0;
1041 Position::Ref_t refLeftTop = Position::kLeftTop;
1042 Position::Ref_t refRightBottom = Position::kLeftTop;
1044 int boxWidth = rRect.getWidth();
1045 int boxHeight = rRect.getHeight();
1047 // Position of the left top corner
1048 if( rLeftTop == "lefttop" )
1052 refLeftTop = Position::kLeftTop;
1054 else if( rLeftTop == "righttop" )
1056 left = xPos - boxWidth + 1;
1058 refLeftTop = Position::kRightTop;
1060 else if( rLeftTop == "leftbottom" )
1063 top = yPos - boxHeight + 1;
1064 refLeftTop = Position::kLeftBottom;
1066 else if( rLeftTop == "rightbottom" )
1068 left = xPos - boxWidth + 1;
1069 top = yPos - boxHeight + 1;
1070 refLeftTop = Position::kRightBottom;
1073 // Position of the right bottom corner
1074 if( rRightBottom == "lefttop" )
1076 right = xPos + width - 1;
1077 bottom = yPos + height - 1;
1078 refRightBottom = Position::kLeftTop;
1080 else if( rRightBottom == "righttop" )
1082 right = xPos + width - boxWidth;
1083 bottom = yPos + height - 1;
1084 refRightBottom = Position::kRightTop;
1086 else if( rRightBottom == "leftbottom" )
1088 right = xPos + width - 1;
1089 bottom = yPos + height - boxHeight;
1090 refRightBottom = Position::kLeftBottom;
1092 else if( rRightBottom == "rightbottom" )
1094 right = xPos + width - boxWidth;
1095 bottom = yPos + height - boxHeight;
1096 refRightBottom = Position::kRightBottom;
1099 // In "keep ratio" mode, overwrite the previously computed values with the
1101 // XXX: this coupling between makePosition and the Position class should
1106 right = xPos + width;
1111 bottom = yPos + height;
1114 return Position( left, top, right, bottom, rRect, refLeftTop,
1115 refRightBottom, xKeepRatio, yKeepRatio );
1119 GenericFont *Builder::getFont( const string &fontId )
1121 GenericFont *pFont = m_pTheme->getFontById(fontId);
1122 if( !pFont && fontId == "defaultfont" )
1124 // Get the resource path and try to load the default font
1125 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
1126 const list<string> &resPath = pOSFactory->getResourcePath();
1127 const string &sep = pOSFactory->getDirSeparator();
1129 list<string>::const_iterator it;
1130 for( it = resPath.begin(); it != resPath.end(); it++ )
1132 string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
1133 pFont = new FT2Font( getIntf(), path, 12 );
1136 // Font loaded successfully
1137 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
1148 msg_Err( getIntf(), "failed to open the default font" );
1155 string Builder::getFilePath( const string &rFileName ) const
1157 OSFactory *pFactory = OSFactory::instance( getIntf() );
1158 const string &sep = pFactory->getDirSeparator();
1160 string file = rFileName;
1161 if( file.find( "\\" ) != string::npos )
1163 // For skins to be valid on both Linux and Win32,
1164 // slash should be used as path separator for both OSs.
1165 msg_Warn( getIntf(), "use of '/' is preferred to '\\' for paths" );
1167 while( ( pos = file.find( "\\" ) ) != string::npos )
1173 while( ( pos = file.find( "/" ) ) != string::npos )
1174 file.replace( pos, 1, sep );
1177 return m_path + sep + sFromLocale( file );
1182 Bezier *Builder::getPoints( const char *pTag ) const
1184 vector<float> xBez, yBez;
1188 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
1193 xBez.push_back( x );
1194 yBez.push_back( y );
1200 if( *(pTag++) != ',' )
1206 // Create the Bezier curve
1207 return new Bezier( getIntf(), xBez, yBez );
1211 uint32_t Builder::getColor( const string &rVal ) const
1213 // Check it the value is a registered constant
1214 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1215 string val = pInterpreter->getConstant( rVal );
1217 // Convert to an int value
1218 return SkinParser::convertColor( val.c_str() );