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>
63 Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData,
64 const string &rPath ):
65 SkinObject( pIntf ), m_rData( rData ), m_path( rPath ), m_pTheme( NULL )
67 m_pImageHandler = image_HandlerCreate( pIntf );
72 if( m_pImageHandler ) image_HandlerDelete( m_pImageHandler );
75 CmdGeneric *Builder::parseAction( const string &rAction )
77 return Interpreter::instance( getIntf() )->parseAction( rAction, m_pTheme );
80 template<class T> inline
81 void Builder::add_objects(const std::list<T> &list,
82 void (Builder::*addfn)(const T &))
84 typename std::list<T>::const_iterator i;
85 for( i = list.begin(); i != list.end(); ++i )
89 Theme *Builder::build()
91 #define ADD_OBJECTS( type ) \
92 add_objects(m_rData.m_list##type,&Builder::add##type)
94 m_pTheme = new (std::nothrow) Theme( getIntf() );
95 if( m_pTheme == NULL )
98 // Create everything from the data in the XML
100 ADD_OBJECTS( IniFile );
101 ADD_OBJECTS( Bitmap );
102 ADD_OBJECTS( SubBitmap );
103 ADD_OBJECTS( BitmapFont );
105 ADD_OBJECTS( Window );
106 // XXX: PopupMenus are created after the windows, so that the Win32Factory
107 // (at least) can give a valid window handle to the OSPopup objects
108 ADD_OBJECTS( PopupMenu );
109 ADD_OBJECTS( Layout );
110 ADD_OBJECTS( Panel );
111 ADD_OBJECTS( Anchor );
112 ADD_OBJECTS( Button );
113 ADD_OBJECTS( Checkbox );
114 ADD_OBJECTS( Image );
116 ADD_OBJECTS( RadialSlider );
117 ADD_OBJECTS( Slider );
120 ADD_OBJECTS( Video );
121 // MenuItems must be created after all the rest, so that the IDs of the
122 // other elements exist and can be parsed in the actions
123 ADD_OBJECTS( MenuItem );
124 ADD_OBJECTS( MenuSeparator );
132 // Macro to get a bitmap by its ID in the builder
133 #define GET_BMP( pBmp, id ) \
136 pBmp = m_pTheme->getBitmapById(id); \
139 msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
145 // Macro to get the parent box of a control, given the panel ID
146 #define GET_BOX( pRect, id, pLayout ) \
148 pRect = &pLayout->getRect(); \
151 const Position *pParent = \
152 m_pTheme->getPositionById( rData.m_panelId ); \
153 if( pParent == NULL ) \
155 msg_Err( getIntf(), "parent panel could not be found: %s", \
156 rData.m_panelId.c_str() ); \
163 void Builder::addTheme( const BuilderData::Theme &rData )
165 WindowManager &rManager = m_pTheme->getWindowManager();
166 rManager.setMagnetValue( rData.m_magnet );
167 rManager.setAlphaValue( rData.m_alpha );
168 rManager.setMoveAlphaValue( rData.m_moveAlpha );
169 GenericFont *pFont = getFont( rData.m_tooltipfont );
172 rManager.createTooltip( *pFont );
176 msg_Warn( getIntf(), "invalid tooltip font: %s",
177 rData.m_tooltipfont.c_str() );
182 void Builder::addIniFile( const BuilderData::IniFile &rData )
184 // Parse the INI file
185 string full_path = getFilePath( rData.m_file );
186 if( !full_path.size() )
189 IniFile iniFile( getIntf(), rData.m_id, full_path );
194 void Builder::addBitmap( const BuilderData::Bitmap &rData )
196 string full_path = getFilePath( rData.m_fileName );
197 if( !full_path.size() )
200 GenericBitmap *pBmp =
201 new FileBitmap( getIntf(), m_pImageHandler,
202 full_path, rData.m_alphaColor,
203 rData.m_nbFrames, rData.m_fps, rData.m_nbLoops );
204 if( !pBmp->getData() )
210 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
214 void Builder::addSubBitmap( const BuilderData::SubBitmap &rData )
216 if( m_pTheme->m_bitmaps.find( rData.m_id ) != m_pTheme->m_bitmaps.end() )
218 msg_Dbg( getIntf(), "bitmap %s already exists", rData.m_id.c_str() );
222 // Get the parent bitmap
223 GenericBitmap *pParentBmp = NULL;
224 GET_BMP( pParentBmp, rData.m_parent );
226 // Copy a region of the parent bitmap to the new one
228 new BitmapImpl( getIntf(), rData.m_width, rData.m_height,
229 rData.m_nbFrames, rData.m_fps, rData.m_nbLoops );
230 bool res = pBmp->drawBitmap( *pParentBmp, rData.m_x, rData.m_y, 0, 0,
231 rData.m_width, rData.m_height );
234 // Invalid sub-bitmap
236 msg_Warn( getIntf(), "sub-bitmap %s ignored", rData.m_id.c_str() );
239 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
243 void Builder::addBitmapFont( const BuilderData::BitmapFont &rData )
245 if( m_pTheme->m_fonts.find( rData.m_id ) != m_pTheme->m_fonts.end() )
247 msg_Dbg( getIntf(), "font %s already exists", rData.m_id.c_str() );
251 string full_path = getFilePath( rData.m_file );
252 if( !full_path.size() )
255 GenericBitmap *pBmp =
256 new FileBitmap( getIntf(), m_pImageHandler, full_path, 0 );
257 if( !pBmp->getData() )
264 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
266 GenericFont *pFont = new BitmapFont( getIntf(), *pBmp, rData.m_type );
269 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
278 void Builder::addFont( const BuilderData::Font &rData )
280 string full_path = getFilePath( rData.m_fontFile );
281 if( !full_path.size() )
284 // Try to load the font from the theme directory
285 GenericFont *pFont = new FT2Font( getIntf(), full_path, rData.m_size );
288 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
294 // Font not found; try in the resource path
295 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
296 const list<string> &resPath = pOSFactory->getResourcePath();
297 const string &sep = pOSFactory->getDirSeparator();
299 list<string>::const_iterator it;
300 for( it = resPath.begin(); it != resPath.end(); it++ )
302 string path = (*it) + sep + "fonts" + sep + rData.m_fontFile;
303 pFont = new FT2Font( getIntf(), path, rData.m_size );
306 // Font loaded successfully
307 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
319 void Builder::addPopupMenu( const BuilderData::PopupMenu &rData )
321 Popup *pPopup = new Popup( getIntf(), m_pTheme->getWindowManager() );
323 m_pTheme->m_popups[rData.m_id] = PopupPtr( pPopup );
327 void Builder::addMenuItem( const BuilderData::MenuItem &rData )
329 Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
332 msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
336 CmdGeneric *pCommand = parseAction( rData.m_action );
337 if( pCommand == NULL )
339 msg_Err( getIntf(), "invalid action: %s", rData.m_action.c_str() );
343 pPopup->addItem( rData.m_label, *pCommand, rData.m_pos );
347 void Builder::addMenuSeparator( const BuilderData::MenuSeparator &rData )
349 Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
352 msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
356 pPopup->addSeparator( rData.m_pos );
360 void Builder::addWindow( const BuilderData::Window &rData )
363 new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
364 m_pTheme->getWindowManager(),
365 rData.m_dragDrop, rData.m_playOnDrop,
368 m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
370 if( rData.m_id == "fullscreenController" )
371 VoutManager::instance( getIntf())->registerFSC( pWin );
375 void Builder::addLayout( const BuilderData::Layout &rData )
377 TopWindow *pWin = m_pTheme->getWindowById( rData.m_windowId );
380 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
384 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
385 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
386 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
388 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
390 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
392 minWidth, maxWidth, minHeight,
394 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
396 // Attach the layout to its window
397 m_pTheme->getWindowManager().addLayout( *pWin, *pLayout );
401 void Builder::addAnchor( const BuilderData::Anchor &rData )
403 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
404 if( pLayout == NULL )
406 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
410 Bezier *pCurve = getPoints( rData.m_points.c_str() );
413 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
414 rData.m_points.c_str() );
417 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
419 // Compute the position of the anchor
420 const Position pos = makePosition( rData.m_leftTop, rData.m_leftTop,
421 rData.m_xPos, rData.m_yPos,
424 pLayout->getRect() );
426 Anchor *pAnc = new Anchor( getIntf(), pos, rData.m_range, rData.m_priority,
428 pLayout->addAnchor( pAnc );
432 void Builder::addButton( const BuilderData::Button &rData )
434 // Get the bitmaps of the button
435 GenericBitmap *pBmpUp = NULL;
436 GET_BMP( pBmpUp, rData.m_upId );
438 GenericBitmap *pBmpDown = pBmpUp;
439 GET_BMP( pBmpDown, rData.m_downId );
441 GenericBitmap *pBmpOver = pBmpUp;
442 GET_BMP( pBmpOver, rData.m_overId );
444 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
445 if( pLayout == NULL )
447 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
451 CmdGeneric *pCommand = parseAction( rData.m_actionId );
452 if( pCommand == NULL )
454 msg_Err( getIntf(), "invalid action: %s", rData.m_actionId.c_str() );
458 // Get the visibility variable
459 // XXX check when it is null
460 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
461 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
463 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
464 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
465 UString( getIntf(), rData.m_help.c_str() ), pVisible );
466 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
468 // Compute the position of the control
469 // XXX (we suppose all the images have the same size...)
470 const GenericRect *pRect;
471 GET_BOX( pRect, rData.m_panelId , pLayout);
472 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
473 rData.m_xPos, rData.m_yPos,
475 pBmpUp->getHeight() / pBmpUp->getNbFrames(),
476 *pRect, rData.m_xKeepRatio, rData.m_yKeepRatio );
478 pLayout->addControl( pButton, pos, rData.m_layer );
482 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
484 // Get the bitmaps of the checkbox
485 GenericBitmap *pBmpUp1 = NULL;
486 GET_BMP( pBmpUp1, rData.m_up1Id );
488 GenericBitmap *pBmpDown1 = pBmpUp1;
489 GET_BMP( pBmpDown1, rData.m_down1Id );
491 GenericBitmap *pBmpOver1 = pBmpUp1;
492 GET_BMP( pBmpOver1, rData.m_over1Id );
494 GenericBitmap *pBmpUp2 = NULL;
495 GET_BMP( pBmpUp2, rData.m_up2Id );
497 GenericBitmap *pBmpDown2 = pBmpUp2;
498 GET_BMP( pBmpDown2, rData.m_down2Id );
500 GenericBitmap *pBmpOver2 = pBmpUp2;
501 GET_BMP( pBmpOver2, rData.m_over2Id );
503 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
504 if( pLayout == NULL )
506 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
510 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
511 if( pCommand1 == NULL )
513 msg_Err( getIntf(), "invalid action: %s", rData.m_action1.c_str() );
517 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
518 if( pCommand2 == NULL )
520 msg_Err( getIntf(), "invalid action: %s", rData.m_action2.c_str() );
524 // Get the state variable
525 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
526 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
529 // TODO: default state
533 // Get the visibility variable
534 // XXX check when it is null
535 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
537 // Create the control
538 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
539 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
540 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
541 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
542 UString( getIntf(), rData.m_help.c_str() ), pVisible );
543 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
545 // Compute the position of the control
546 // XXX (we suppose all the images have the same size...)
547 const GenericRect *pRect;
548 GET_BOX( pRect, rData.m_panelId , pLayout);
549 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
550 rData.m_xPos, rData.m_yPos,
552 pBmpUp1->getHeight() / pBmpUp1->getNbFrames(),
553 *pRect, rData.m_xKeepRatio, rData.m_yKeepRatio );
555 pLayout->addControl( pCheckbox, pos, rData.m_layer );
559 void Builder::addImage( const BuilderData::Image &rData )
561 GenericBitmap *pBmp = NULL;
562 GET_BMP( pBmp, rData.m_bmpId );
564 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
565 if( pLayout == NULL )
567 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
571 TopWindow *pWindow = m_pTheme->getWindowById( rData.m_windowId );
572 if( pWindow == NULL )
574 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
578 CmdGeneric *pCommand = parseAction( rData.m_action2Id );
579 if( pCommand == NULL )
581 msg_Err( getIntf(), "invalid action: %s", rData.m_action2Id.c_str() );
585 // Get the visibility variable
586 // XXX check when it is null
587 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
588 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
590 CtrlImage::resize_t resizeMethod =
591 rData.m_resize == "scale" ? CtrlImage::kScale :
592 rData.m_resize == "mosaic" ? CtrlImage::kMosaic :
593 CtrlImage::kScaleAndRatioPreserved;
594 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, *pCommand,
595 resizeMethod, UString( getIntf(), rData.m_help.c_str() ), pVisible,
597 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
599 // Compute the position of the control
600 const GenericRect *pRect;
601 GET_BOX( pRect, rData.m_panelId , pLayout);
602 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
603 rData.m_xPos, rData.m_yPos,
604 pBmp->getWidth(), pBmp->getHeight(),
605 *pRect, rData.m_xKeepRatio,
606 rData.m_yKeepRatio );
608 if( rData.m_actionId == "move" )
610 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
611 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
613 m_pTheme->m_controls[rData.m_id + "_move"] = CtrlGenericPtr( pMove );
614 pLayout->addControl( pMove, pos, rData.m_layer );
616 else if( rData.m_actionId == "resizeS" )
618 CtrlResize *pResize = new CtrlResize( getIntf(),
619 m_pTheme->getWindowManager(), *pImage, *pLayout,
620 UString( getIntf(), rData.m_help.c_str() ), pVisible,
621 WindowManager::kResizeS );
622 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
623 pLayout->addControl( pResize, pos, rData.m_layer );
625 else if( rData.m_actionId == "resizeE" )
627 CtrlResize *pResize = new CtrlResize( getIntf(),
628 m_pTheme->getWindowManager(), *pImage, *pLayout,
629 UString( getIntf(), rData.m_help.c_str() ), pVisible,
630 WindowManager::kResizeE );
631 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
632 pLayout->addControl( pResize, pos, rData.m_layer );
634 else if( rData.m_actionId == "resizeSE" )
636 CtrlResize *pResize = new CtrlResize( getIntf(),
637 m_pTheme->getWindowManager(), *pImage, *pLayout,
638 UString( getIntf(), rData.m_help.c_str() ), pVisible,
639 WindowManager::kResizeSE );
640 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
641 pLayout->addControl( pResize, pos, rData.m_layer );
645 pLayout->addControl( pImage, pos, rData.m_layer );
650 void Builder::addPanel( const BuilderData::Panel &rData )
652 // This method makes the assumption that the Panels are created in the
653 // order of the XML, because each child Panel expects its parent Panel
654 // in order to be fully created
656 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
657 if( pLayout == NULL )
659 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
663 const GenericRect *pRect;
664 GET_BOX( pRect, rData.m_panelId , pLayout);
666 new Position( makePosition( rData.m_leftTop, rData.m_rightBottom,
667 rData.m_xPos, rData.m_yPos,
668 rData.m_width, rData.m_height,
669 *pRect, rData.m_xKeepRatio,
670 rData.m_yKeepRatio ) );
671 m_pTheme->m_positions[rData.m_id] = PositionPtr( pPos );
675 void Builder::addText( const BuilderData::Text &rData )
677 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
678 if( pLayout == NULL )
680 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
684 GenericFont *pFont = getFont( rData.m_fontId );
687 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
691 // Convert the scrolling mode
692 CtrlText::Scrolling_t scrolling;
693 if( rData.m_scrolling == "auto" )
694 scrolling = CtrlText::kAutomatic;
695 else if( rData.m_scrolling == "manual" )
696 scrolling = CtrlText::kManual;
697 else if( rData.m_scrolling == "none" )
698 scrolling = CtrlText::kNone;
701 msg_Err( getIntf(), "invalid scrolling mode: %s",
702 rData.m_scrolling.c_str() );
706 // Convert the alignment
707 CtrlText::Align_t alignment;
708 if( rData.m_alignment == "left" )
709 alignment = CtrlText::kLeft;
710 else if( rData.m_alignment == "center" || rData.m_alignment == "centre" )
711 alignment = CtrlText::kCenter;
712 else if( rData.m_alignment == "right" )
713 alignment = CtrlText::kRight;
716 msg_Err( getIntf(), "invalid alignment: %s",
717 rData.m_alignment.c_str() );
721 // Create a text variable
722 VarText *pVar = new VarText( getIntf() );
723 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
725 // Set the text of the control
726 UString msg( getIntf(), rData.m_text.c_str() );
729 // Get the visibility variable
730 // XXX check when it is null
731 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
732 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
734 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
735 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible,
736 scrolling, alignment );
737 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
739 int height = pFont->getSize();
741 // Compute the position of the control
742 const GenericRect *pRect;
743 GET_BOX( pRect, rData.m_panelId , pLayout);
744 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
745 rData.m_xPos, rData.m_yPos,
746 rData.m_width, height, *pRect,
747 rData.m_xKeepRatio, rData.m_yKeepRatio );
749 pLayout->addControl( pText, pos, rData.m_layer );
754 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
756 // Get the bitmaps of the slider
757 GenericBitmap *pSeq = NULL;
758 GET_BMP( pSeq, rData.m_sequence );
760 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
761 if( pLayout == NULL )
763 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
767 // Get the variable associated to the slider
768 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
769 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
772 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
776 // Get the visibility variable
777 // XXX check when it is null
778 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
780 // Create the control
781 CtrlRadialSlider *pRadial =
782 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
783 rData.m_minAngle, rData.m_maxAngle,
784 UString( getIntf(), rData.m_help.c_str() ),
786 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
788 // XXX: resizing is not supported
789 // Compute the position of the control
790 const GenericRect *pRect;
791 GET_BOX( pRect, rData.m_panelId , pLayout);
792 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
793 rData.m_xPos, rData.m_yPos,
795 pSeq->getHeight() / rData.m_nbImages,
797 rData.m_xKeepRatio, rData.m_yKeepRatio );
799 pLayout->addControl( pRadial, pos, rData.m_layer );
803 void Builder::addSlider( const BuilderData::Slider &rData )
805 // Add the background first, so that we will still have something almost
806 // functional if the cursor cannot be created properly (this happens for
807 // some winamp2 skins, where the images of the cursor are not always
810 // Get the bitmaps of the background
811 GenericBitmap *pBgImage = NULL;
812 if( rData.m_imageId != "none" )
814 GET_BMP( pBgImage, rData.m_imageId );
817 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
818 if( pLayout == NULL )
820 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
824 Bezier *pCurve = getPoints( rData.m_points.c_str() );
827 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
828 rData.m_points.c_str() );
831 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
833 // Get the visibility variable
834 // XXX check when it is null
835 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
836 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
838 // Get the variable associated to the slider
839 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
842 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
846 // Create the background control
847 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(),
848 *pCurve, *pVar, rData.m_thickness, pBgImage, rData.m_nbHoriz,
849 rData.m_nbVert, rData.m_padHoriz, rData.m_padVert,
850 pVisible, UString( getIntf(), rData.m_help.c_str() ) );
851 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
853 // Compute the position of the control
854 const GenericRect *pRect;
855 GET_BOX( pRect, rData.m_panelId , pLayout);
856 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
857 rData.m_xPos, rData.m_yPos,
858 pCurve->getWidth(), pCurve->getHeight(),
860 rData.m_xKeepRatio, rData.m_yKeepRatio );
862 pLayout->addControl( pBackground, pos, rData.m_layer );
864 // Get the bitmaps of the cursor
865 GenericBitmap *pBmpUp = NULL;
866 GET_BMP( pBmpUp, rData.m_upId );
868 GenericBitmap *pBmpDown = pBmpUp;
869 GET_BMP( pBmpDown, rData.m_downId );
871 GenericBitmap *pBmpOver = pBmpUp;
872 GET_BMP( pBmpOver, rData.m_overId );
874 // Create the cursor control
875 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
876 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
877 UString( getIntf(), rData.m_tooltip.c_str() ),
878 UString( getIntf(), rData.m_help.c_str() ) );
879 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
881 pLayout->addControl( pCursor, pos, rData.m_layer );
883 // Associate the cursor to the background
884 pBackground->associateCursor( *pCursor );
888 void Builder::addList( const BuilderData::List &rData )
890 // Get the background bitmap, if any
891 GenericBitmap *pBgBmp = NULL;
892 GET_BMP( pBgBmp, rData.m_bgImageId );
894 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
895 if( pLayout == NULL )
897 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
901 GenericFont *pFont = getFont( rData.m_fontId );
904 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
908 // Get the list variable
909 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
910 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
913 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
917 // Get the visibility variable
918 // XXX check when it is null
919 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
921 // Get the color values
922 uint32_t fgColor = getColor( rData.m_fgColor );
923 uint32_t playColor = getColor( rData.m_playColor );
924 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
925 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
926 uint32_t selColor = getColor( rData.m_selColor );
928 // Create the list control
929 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
930 fgColor, playColor, bgColor1, bgColor2, selColor,
931 UString( getIntf(), rData.m_help.c_str() ), pVisible );
932 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
934 // Compute the position of the control
935 const GenericRect *pRect;
936 GET_BOX( pRect, rData.m_panelId , pLayout);
937 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
938 rData.m_xPos, rData.m_yPos,
939 rData.m_width, rData.m_height,
941 rData.m_xKeepRatio, rData.m_yKeepRatio );
943 pLayout->addControl( pList, pos, rData.m_layer );
946 void Builder::addTree( const BuilderData::Tree &rData )
948 // Get the bitmaps, if any
949 GenericBitmap *pBgBmp = NULL;
950 GenericBitmap *pItemBmp = NULL;
951 GenericBitmap *pOpenBmp = NULL;
952 GenericBitmap *pClosedBmp = NULL;
953 GET_BMP( pBgBmp, rData.m_bgImageId );
954 GET_BMP( pItemBmp, rData.m_itemImageId );
955 GET_BMP( pOpenBmp, rData.m_openImageId );
956 GET_BMP( pClosedBmp, rData.m_closedImageId );
958 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
959 if( pLayout == NULL )
961 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
965 GenericFont *pFont = getFont( rData.m_fontId );
968 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
972 // Get the list variable
973 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
974 VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
977 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
981 // Get the visibility variable
982 // XXX check when it is null
983 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
984 VarBool *pFlat = pInterpreter->getVarBool( rData.m_flat, m_pTheme );
986 // Get the color values
987 uint32_t fgColor = getColor( rData.m_fgColor );
988 uint32_t playColor = getColor( rData.m_playColor );
989 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
990 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
991 uint32_t selColor = getColor( rData.m_selColor );
993 // Create the list control
994 CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
995 pItemBmp, pOpenBmp, pClosedBmp,
996 fgColor, playColor, bgColor1, bgColor2, selColor,
997 UString( getIntf(), rData.m_help.c_str() ), pVisible, pFlat );
998 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
1000 // Compute the position of the control
1001 const GenericRect *pRect;
1002 GET_BOX( pRect, rData.m_panelId , pLayout);
1003 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
1004 rData.m_xPos, rData.m_yPos,
1005 rData.m_width, rData.m_height,
1007 rData.m_xKeepRatio, rData.m_yKeepRatio );
1009 pLayout->addControl( pTree, pos, rData.m_layer );
1012 void Builder::addVideo( const BuilderData::Video &rData )
1014 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
1015 if( pLayout == NULL )
1017 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
1021 BuilderData::Video Data = rData;
1022 if( Data.m_autoResize )
1024 // force autoresize to false if the control is not able to
1025 // freely resize within its container
1026 if( Data.m_xKeepRatio || Data.m_yKeepRatio ||
1027 !( Data.m_leftTop == "lefttop" &&
1028 Data.m_rightBottom == "rightbottom" ) )
1031 "video: resize policy and autoresize are not compatible" );
1032 Data.m_autoResize = false;
1036 // Get the visibility variable
1037 // XXX check when it is null
1038 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1039 VarBool *pVisible = pInterpreter->getVarBool( Data.m_visible, m_pTheme );
1041 CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
1042 Data.m_autoResize, UString( getIntf(), Data.m_help.c_str() ),
1044 m_pTheme->m_controls[Data.m_id] = CtrlGenericPtr( pVideo );
1046 // Compute the position of the control
1047 const GenericRect *pRect;
1048 GET_BOX( pRect, rData.m_panelId , pLayout);
1049 const Position pos = makePosition( Data.m_leftTop, Data.m_rightBottom,
1050 Data.m_xPos, Data.m_yPos,
1051 Data.m_width, Data.m_height,
1053 Data.m_xKeepRatio, Data.m_yKeepRatio );
1055 pLayout->addControl( pVideo, pos, Data.m_layer );
1059 const Position Builder::makePosition( const string &rLeftTop,
1060 const string &rRightBottom,
1061 int xPos, int yPos, int width,
1062 int height, const GenericRect &rRect,
1063 bool xKeepRatio, bool yKeepRatio ) const
1065 int left = 0, top = 0, right = 0, bottom = 0;
1066 Position::Ref_t refLeftTop = Position::kLeftTop;
1067 Position::Ref_t refRightBottom = Position::kLeftTop;
1069 int boxWidth = rRect.getWidth();
1070 int boxHeight = rRect.getHeight();
1072 // Position of the left top corner
1073 if( rLeftTop == "lefttop" )
1077 refLeftTop = Position::kLeftTop;
1079 else if( rLeftTop == "righttop" )
1081 left = xPos - boxWidth + 1;
1083 refLeftTop = Position::kRightTop;
1085 else if( rLeftTop == "leftbottom" )
1088 top = yPos - boxHeight + 1;
1089 refLeftTop = Position::kLeftBottom;
1091 else if( rLeftTop == "rightbottom" )
1093 left = xPos - boxWidth + 1;
1094 top = yPos - boxHeight + 1;
1095 refLeftTop = Position::kRightBottom;
1098 // Position of the right bottom corner
1099 if( rRightBottom == "lefttop" )
1101 right = xPos + width - 1;
1102 bottom = yPos + height - 1;
1103 refRightBottom = Position::kLeftTop;
1105 else if( rRightBottom == "righttop" )
1107 right = xPos + width - boxWidth;
1108 bottom = yPos + height - 1;
1109 refRightBottom = Position::kRightTop;
1111 else if( rRightBottom == "leftbottom" )
1113 right = xPos + width - 1;
1114 bottom = yPos + height - boxHeight;
1115 refRightBottom = Position::kLeftBottom;
1117 else if( rRightBottom == "rightbottom" )
1119 right = xPos + width - boxWidth;
1120 bottom = yPos + height - boxHeight;
1121 refRightBottom = Position::kRightBottom;
1124 // In "keep ratio" mode, overwrite the previously computed values with the
1126 // XXX: this coupling between makePosition and the Position class should
1131 right = xPos + width;
1136 bottom = yPos + height;
1139 return Position( left, top, right, bottom, rRect, refLeftTop,
1140 refRightBottom, xKeepRatio, yKeepRatio );
1144 GenericFont *Builder::getFont( const string &fontId )
1146 GenericFont *pFont = m_pTheme->getFontById(fontId);
1147 if( !pFont && fontId == "defaultfont" )
1149 // Get the resource path and try to load the default font
1150 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
1151 const list<string> &resPath = pOSFactory->getResourcePath();
1152 const string &sep = pOSFactory->getDirSeparator();
1154 list<string>::const_iterator it;
1155 for( it = resPath.begin(); it != resPath.end(); it++ )
1157 string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
1158 pFont = new FT2Font( getIntf(), path, 12 );
1161 // Font loaded successfully
1162 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
1173 msg_Err( getIntf(), "failed to open the default font" );
1180 string Builder::getFilePath( const string &rFileName ) const
1182 OSFactory *pFactory = OSFactory::instance( getIntf() );
1183 const string &sep = pFactory->getDirSeparator();
1185 string file = rFileName;
1186 if( file.find( "\\" ) != string::npos )
1188 // For skins to be valid on both Linux and Win32,
1189 // slash should be used as path separator for both OSs.
1190 msg_Warn( getIntf(), "use of '/' is preferred to '\\' for paths" );
1192 while( ( pos = file.find( "\\" ) ) != string::npos )
1198 while( ( pos = file.find( "/" ) ) != string::npos )
1199 file.replace( pos, 1, sep );
1202 string full_path = m_path + sep + sFromLocale( file );
1204 // check that the file exists and can be read
1205 if( ifstream( full_path.c_str() ).fail() )
1207 msg_Err( getIntf(), "missing file: %s", file.c_str() );
1216 Bezier *Builder::getPoints( const char *pTag ) const
1218 vector<float> xBez, yBez;
1222 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
1227 xBez.push_back( x );
1228 yBez.push_back( y );
1234 if( *(pTag++) != ',' )
1240 // Create the Bezier curve
1241 return new Bezier( getIntf(), xBez, yBez );
1245 uint32_t Builder::getColor( const string &rVal ) const
1247 // Check it the value is a registered constant
1248 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1249 string val = pInterpreter->getConstant( rVal );
1251 // Convert to an int value
1252 return SkinParser::convertColor( val.c_str() );