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 :
578 rData.m_resize == "mosaic" ? CtrlImage::kMosaic :
579 CtrlImage::kScaleAndRatioPreserved;
580 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, *pCommand,
581 resizeMethod, UString( getIntf(), rData.m_help.c_str() ), pVisible,
583 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
585 // Compute the position of the control
586 const GenericRect *pRect;
587 GET_BOX( pRect, rData.m_panelId , pLayout);
588 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
589 rData.m_xPos, rData.m_yPos,
590 pBmp->getWidth(), pBmp->getHeight(),
591 *pRect, rData.m_xKeepRatio,
592 rData.m_yKeepRatio );
594 if( rData.m_actionId == "move" )
596 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
597 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
599 m_pTheme->m_controls[rData.m_id + "_move"] = CtrlGenericPtr( pMove );
600 pLayout->addControl( pMove, pos, rData.m_layer );
602 else if( rData.m_actionId == "resizeS" )
604 CtrlResize *pResize = new CtrlResize( getIntf(),
605 m_pTheme->getWindowManager(), *pImage, *pLayout,
606 UString( getIntf(), rData.m_help.c_str() ), pVisible,
607 WindowManager::kResizeS );
608 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
609 pLayout->addControl( pResize, pos, rData.m_layer );
611 else if( rData.m_actionId == "resizeE" )
613 CtrlResize *pResize = new CtrlResize( getIntf(),
614 m_pTheme->getWindowManager(), *pImage, *pLayout,
615 UString( getIntf(), rData.m_help.c_str() ), pVisible,
616 WindowManager::kResizeE );
617 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
618 pLayout->addControl( pResize, pos, rData.m_layer );
620 else if( rData.m_actionId == "resizeSE" )
622 CtrlResize *pResize = new CtrlResize( getIntf(),
623 m_pTheme->getWindowManager(), *pImage, *pLayout,
624 UString( getIntf(), rData.m_help.c_str() ), pVisible,
625 WindowManager::kResizeSE );
626 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
627 pLayout->addControl( pResize, pos, rData.m_layer );
631 pLayout->addControl( pImage, pos, rData.m_layer );
636 void Builder::addPanel( const BuilderData::Panel &rData )
638 // This method makes the assumption that the Panels are created in the
639 // order of the XML, because each child Panel expects its parent Panel
640 // in order to be fully created
642 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
643 if( pLayout == NULL )
645 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
649 const GenericRect *pRect;
650 GET_BOX( pRect, rData.m_panelId , pLayout);
652 new Position( makePosition( rData.m_leftTop, rData.m_rightBottom,
653 rData.m_xPos, rData.m_yPos,
654 rData.m_width, rData.m_height,
655 *pRect, rData.m_xKeepRatio,
656 rData.m_yKeepRatio ) );
657 m_pTheme->m_positions[rData.m_id] = PositionPtr( pPos );
661 void Builder::addText( const BuilderData::Text &rData )
663 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
664 if( pLayout == NULL )
666 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
670 GenericFont *pFont = getFont( rData.m_fontId );
673 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
677 // Convert the scrolling mode
678 CtrlText::Scrolling_t scrolling;
679 if( rData.m_scrolling == "auto" )
680 scrolling = CtrlText::kAutomatic;
681 else if( rData.m_scrolling == "manual" )
682 scrolling = CtrlText::kManual;
683 else if( rData.m_scrolling == "none" )
684 scrolling = CtrlText::kNone;
687 msg_Err( getIntf(), "invalid scrolling mode: %s",
688 rData.m_scrolling.c_str() );
692 // Convert the alignment
693 CtrlText::Align_t alignment;
694 if( rData.m_alignment == "left" )
695 alignment = CtrlText::kLeft;
696 else if( rData.m_alignment == "center" || rData.m_alignment == "centre" )
697 alignment = CtrlText::kCenter;
698 else if( rData.m_alignment == "right" )
699 alignment = CtrlText::kRight;
702 msg_Err( getIntf(), "invalid alignment: %s",
703 rData.m_alignment.c_str() );
707 // Create a text variable
708 VarText *pVar = new VarText( getIntf() );
709 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
711 // Get the visibility variable
712 // XXX check when it is null
713 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
714 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
716 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
717 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible,
718 scrolling, alignment );
719 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
721 int height = pFont->getSize();
723 // Compute the position of the control
724 const GenericRect *pRect;
725 GET_BOX( pRect, rData.m_panelId , pLayout);
726 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
727 rData.m_xPos, rData.m_yPos,
728 rData.m_width, height, *pRect,
729 rData.m_xKeepRatio, rData.m_yKeepRatio );
731 pLayout->addControl( pText, pos, rData.m_layer );
733 // Set the text of the control
734 UString msg( getIntf(), rData.m_text.c_str() );
739 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
741 // Get the bitmaps of the slider
742 GenericBitmap *pSeq = NULL;
743 GET_BMP( pSeq, rData.m_sequence );
745 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
746 if( pLayout == NULL )
748 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
752 // Get the variable associated to the slider
753 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
754 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
757 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
761 // Get the visibility variable
762 // XXX check when it is null
763 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
765 // Create the control
766 CtrlRadialSlider *pRadial =
767 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
768 rData.m_minAngle, rData.m_maxAngle,
769 UString( getIntf(), rData.m_help.c_str() ),
771 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
773 // XXX: resizing is not supported
774 // Compute the position of the control
775 const GenericRect *pRect;
776 GET_BOX( pRect, rData.m_panelId , pLayout);
777 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
778 rData.m_xPos, rData.m_yPos,
780 pSeq->getHeight() / rData.m_nbImages,
782 rData.m_xKeepRatio, rData.m_yKeepRatio );
784 pLayout->addControl( pRadial, pos, rData.m_layer );
788 void Builder::addSlider( const BuilderData::Slider &rData )
790 // Add the background first, so that we will still have something almost
791 // functional if the cursor cannot be created properly (this happens for
792 // some winamp2 skins, where the images of the cursor are not always
795 // Get the bitmaps of the background
796 GenericBitmap *pBgImage = NULL;
797 if( rData.m_imageId != "none" )
799 GET_BMP( pBgImage, rData.m_imageId );
802 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
803 if( pLayout == NULL )
805 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
809 Bezier *pCurve = getPoints( rData.m_points.c_str() );
812 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
813 rData.m_points.c_str() );
816 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
818 // Get the visibility variable
819 // XXX check when it is null
820 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
821 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
823 // Get the variable associated to the slider
824 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
827 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
831 // Create the background control
832 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(),
833 *pCurve, *pVar, rData.m_thickness, pBgImage, rData.m_nbHoriz,
834 rData.m_nbVert, rData.m_padHoriz, rData.m_padVert,
835 pVisible, UString( getIntf(), rData.m_help.c_str() ) );
836 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
838 // Compute the position of the control
839 const GenericRect *pRect;
840 GET_BOX( pRect, rData.m_panelId , pLayout);
841 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
842 rData.m_xPos, rData.m_yPos,
843 pCurve->getWidth(), pCurve->getHeight(),
845 rData.m_xKeepRatio, rData.m_yKeepRatio );
847 pLayout->addControl( pBackground, pos, rData.m_layer );
849 // Get the bitmaps of the cursor
850 GenericBitmap *pBmpUp = NULL;
851 GET_BMP( pBmpUp, rData.m_upId );
853 GenericBitmap *pBmpDown = pBmpUp;
854 GET_BMP( pBmpDown, rData.m_downId );
856 GenericBitmap *pBmpOver = pBmpUp;
857 GET_BMP( pBmpOver, rData.m_overId );
859 // Create the cursor control
860 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
861 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
862 UString( getIntf(), rData.m_tooltip.c_str() ),
863 UString( getIntf(), rData.m_help.c_str() ) );
864 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
866 pLayout->addControl( pCursor, pos, rData.m_layer );
868 // Associate the cursor to the background
869 pBackground->associateCursor( *pCursor );
873 void Builder::addList( const BuilderData::List &rData )
875 // Get the background bitmap, if any
876 GenericBitmap *pBgBmp = NULL;
877 GET_BMP( pBgBmp, rData.m_bgImageId );
879 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
880 if( pLayout == NULL )
882 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
886 GenericFont *pFont = getFont( rData.m_fontId );
889 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
893 // Get the list variable
894 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
895 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
898 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
902 // Get the visibility variable
903 // XXX check when it is null
904 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
906 // Get the color values
907 uint32_t fgColor = getColor( rData.m_fgColor );
908 uint32_t playColor = getColor( rData.m_playColor );
909 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
910 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
911 uint32_t selColor = getColor( rData.m_selColor );
913 // Create the list control
914 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
915 fgColor, playColor, bgColor1, bgColor2, selColor,
916 UString( getIntf(), rData.m_help.c_str() ), pVisible );
917 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
919 // Compute the position of the control
920 const GenericRect *pRect;
921 GET_BOX( pRect, rData.m_panelId , pLayout);
922 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
923 rData.m_xPos, rData.m_yPos,
924 rData.m_width, rData.m_height,
926 rData.m_xKeepRatio, rData.m_yKeepRatio );
928 pLayout->addControl( pList, pos, rData.m_layer );
931 void Builder::addTree( const BuilderData::Tree &rData )
933 // Get the bitmaps, if any
934 GenericBitmap *pBgBmp = NULL;
935 GenericBitmap *pItemBmp = NULL;
936 GenericBitmap *pOpenBmp = NULL;
937 GenericBitmap *pClosedBmp = NULL;
938 GET_BMP( pBgBmp, rData.m_bgImageId );
939 GET_BMP( pItemBmp, rData.m_itemImageId );
940 GET_BMP( pOpenBmp, rData.m_openImageId );
941 GET_BMP( pClosedBmp, rData.m_closedImageId );
943 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
944 if( pLayout == NULL )
946 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
950 GenericFont *pFont = getFont( rData.m_fontId );
953 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
957 // Get the list variable
958 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
959 VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
962 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
966 // Get the visibility variable
967 // XXX check when it is null
968 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
969 VarBool *pFlat = pInterpreter->getVarBool( rData.m_flat, m_pTheme );
971 // Get the color values
972 uint32_t fgColor = getColor( rData.m_fgColor );
973 uint32_t playColor = getColor( rData.m_playColor );
974 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
975 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
976 uint32_t selColor = getColor( rData.m_selColor );
978 // Create the list control
979 CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
980 pItemBmp, pOpenBmp, pClosedBmp,
981 fgColor, playColor, bgColor1, bgColor2, selColor,
982 UString( getIntf(), rData.m_help.c_str() ), pVisible, pFlat );
983 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
985 // Compute the position of the control
986 const GenericRect *pRect;
987 GET_BOX( pRect, rData.m_panelId , pLayout);
988 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
989 rData.m_xPos, rData.m_yPos,
990 rData.m_width, rData.m_height,
992 rData.m_xKeepRatio, rData.m_yKeepRatio );
994 pLayout->addControl( pTree, pos, rData.m_layer );
997 void Builder::addVideo( const BuilderData::Video &rData )
999 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
1000 if( pLayout == NULL )
1002 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
1006 // Get the visibility variable
1007 // XXX check when it is null
1008 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1009 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
1011 CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
1012 rData.m_autoResize, UString( getIntf(), rData.m_help.c_str() ),
1014 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
1016 // if autoresize is true, force the control to resize
1017 BuilderData::Video Data = rData;
1018 if( rData.m_autoResize )
1020 Data.m_leftTop = "lefttop";
1021 Data.m_rightBottom = "rightbottom";
1024 // Compute the position of the control
1025 const GenericRect *pRect;
1026 GET_BOX( pRect, rData.m_panelId , pLayout);
1027 const Position pos = makePosition( Data.m_leftTop, Data.m_rightBottom,
1028 Data.m_xPos, Data.m_yPos,
1029 Data.m_width, Data.m_height,
1031 Data.m_xKeepRatio, Data.m_yKeepRatio );
1033 pLayout->addControl( pVideo, pos, rData.m_layer );
1037 const Position Builder::makePosition( const string &rLeftTop,
1038 const string &rRightBottom,
1039 int xPos, int yPos, int width,
1040 int height, const GenericRect &rRect,
1041 bool xKeepRatio, bool yKeepRatio ) const
1043 int left = 0, top = 0, right = 0, bottom = 0;
1044 Position::Ref_t refLeftTop = Position::kLeftTop;
1045 Position::Ref_t refRightBottom = Position::kLeftTop;
1047 int boxWidth = rRect.getWidth();
1048 int boxHeight = rRect.getHeight();
1050 // Position of the left top corner
1051 if( rLeftTop == "lefttop" )
1055 refLeftTop = Position::kLeftTop;
1057 else if( rLeftTop == "righttop" )
1059 left = xPos - boxWidth + 1;
1061 refLeftTop = Position::kRightTop;
1063 else if( rLeftTop == "leftbottom" )
1066 top = yPos - boxHeight + 1;
1067 refLeftTop = Position::kLeftBottom;
1069 else if( rLeftTop == "rightbottom" )
1071 left = xPos - boxWidth + 1;
1072 top = yPos - boxHeight + 1;
1073 refLeftTop = Position::kRightBottom;
1076 // Position of the right bottom corner
1077 if( rRightBottom == "lefttop" )
1079 right = xPos + width - 1;
1080 bottom = yPos + height - 1;
1081 refRightBottom = Position::kLeftTop;
1083 else if( rRightBottom == "righttop" )
1085 right = xPos + width - boxWidth;
1086 bottom = yPos + height - 1;
1087 refRightBottom = Position::kRightTop;
1089 else if( rRightBottom == "leftbottom" )
1091 right = xPos + width - 1;
1092 bottom = yPos + height - boxHeight;
1093 refRightBottom = Position::kLeftBottom;
1095 else if( rRightBottom == "rightbottom" )
1097 right = xPos + width - boxWidth;
1098 bottom = yPos + height - boxHeight;
1099 refRightBottom = Position::kRightBottom;
1102 // In "keep ratio" mode, overwrite the previously computed values with the
1104 // XXX: this coupling between makePosition and the Position class should
1109 right = xPos + width;
1114 bottom = yPos + height;
1117 return Position( left, top, right, bottom, rRect, refLeftTop,
1118 refRightBottom, xKeepRatio, yKeepRatio );
1122 GenericFont *Builder::getFont( const string &fontId )
1124 GenericFont *pFont = m_pTheme->getFontById(fontId);
1125 if( !pFont && fontId == "defaultfont" )
1127 // Get the resource path and try to load the default font
1128 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
1129 const list<string> &resPath = pOSFactory->getResourcePath();
1130 const string &sep = pOSFactory->getDirSeparator();
1132 list<string>::const_iterator it;
1133 for( it = resPath.begin(); it != resPath.end(); it++ )
1135 string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
1136 pFont = new FT2Font( getIntf(), path, 12 );
1139 // Font loaded successfully
1140 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
1151 msg_Err( getIntf(), "failed to open the default font" );
1158 string Builder::getFilePath( const string &rFileName ) const
1160 OSFactory *pFactory = OSFactory::instance( getIntf() );
1161 const string &sep = pFactory->getDirSeparator();
1163 string file = rFileName;
1164 if( file.find( "\\" ) != string::npos )
1166 // For skins to be valid on both Linux and Win32,
1167 // slash should be used as path separator for both OSs.
1168 msg_Warn( getIntf(), "use of '/' is preferred to '\\' for paths" );
1170 while( ( pos = file.find( "\\" ) ) != string::npos )
1176 while( ( pos = file.find( "/" ) ) != string::npos )
1177 file.replace( pos, 1, sep );
1180 return m_path + sep + sFromLocale( file );
1185 Bezier *Builder::getPoints( const char *pTag ) const
1187 vector<float> xBez, yBez;
1191 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
1196 xBez.push_back( x );
1197 yBez.push_back( y );
1203 if( *(pTag++) != ',' )
1209 // Create the Bezier curve
1210 return new Bezier( getIntf(), xBez, yBez );
1214 uint32_t Builder::getColor( const string &rVal ) const
1216 // Check it the value is a registered constant
1217 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1218 string val = pInterpreter->getConstant( rVal );
1220 // Convert to an int value
1221 return SkinParser::convertColor( val.c_str() );