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/fsc_window.hpp"
34 #include "../src/anchor.hpp"
35 #include "../src/bitmap_font.hpp"
36 #include "../src/ft2_font.hpp"
37 #include "../src/ini_file.hpp"
38 #include "../src/generic_layout.hpp"
39 #include "../src/popup.hpp"
40 #include "../src/theme.hpp"
41 #include "../src/window_manager.hpp"
42 #include "../src/vout_manager.hpp"
43 #include "../commands/cmd_generic.hpp"
44 #include "../controls/ctrl_button.hpp"
45 #include "../controls/ctrl_checkbox.hpp"
46 #include "../controls/ctrl_image.hpp"
47 #include "../controls/ctrl_list.hpp"
48 #include "../controls/ctrl_move.hpp"
49 #include "../controls/ctrl_resize.hpp"
50 #include "../controls/ctrl_slider.hpp"
51 #include "../controls/ctrl_radialslider.hpp"
52 #include "../controls/ctrl_text.hpp"
53 #include "../controls/ctrl_tree.hpp"
54 #include "../controls/ctrl_video.hpp"
55 #include "../utils/bezier.hpp"
56 #include "../utils/position.hpp"
57 #include "../utils/var_bool.hpp"
58 #include "../utils/var_text.hpp"
60 #include <vlc_image.h>
64 Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData,
65 const string &rPath ):
66 SkinObject( pIntf ), m_rData( rData ), m_path( rPath ), m_pTheme( NULL )
68 m_pImageHandler = image_HandlerCreate( pIntf );
73 if( m_pImageHandler ) image_HandlerDelete( m_pImageHandler );
76 CmdGeneric *Builder::parseAction( const string &rAction )
78 return Interpreter::instance( getIntf() )->parseAction( rAction, m_pTheme );
81 template<class T> inline
82 void Builder::add_objects(const std::list<T> &list,
83 void (Builder::*addfn)(const T &))
85 typename std::list<T>::const_iterator i;
86 for( i = list.begin(); i != list.end(); ++i )
90 Theme *Builder::build()
92 #define ADD_OBJECTS( type ) \
93 add_objects(m_rData.m_list##type,&Builder::add##type)
95 m_pTheme = new (std::nothrow) Theme( getIntf() );
96 if( m_pTheme == NULL )
99 // Create everything from the data in the XML
100 ADD_OBJECTS( Theme );
101 ADD_OBJECTS( IniFile );
102 ADD_OBJECTS( Bitmap );
103 ADD_OBJECTS( SubBitmap );
104 ADD_OBJECTS( BitmapFont );
106 ADD_OBJECTS( Window );
107 // XXX: PopupMenus are created after the windows, so that the Win32Factory
108 // (at least) can give a valid window handle to the OSPopup objects
109 ADD_OBJECTS( PopupMenu );
110 ADD_OBJECTS( Layout );
111 ADD_OBJECTS( Panel );
112 ADD_OBJECTS( Anchor );
113 ADD_OBJECTS( Button );
114 ADD_OBJECTS( Checkbox );
115 ADD_OBJECTS( Image );
117 ADD_OBJECTS( RadialSlider );
118 ADD_OBJECTS( Slider );
121 ADD_OBJECTS( Video );
122 // MenuItems must be created after all the rest, so that the IDs of the
123 // other elements exist and can be parsed in the actions
124 ADD_OBJECTS( MenuItem );
125 ADD_OBJECTS( MenuSeparator );
133 // Macro to get a bitmap by its ID in the builder
134 #define GET_BMP( pBmp, id ) \
137 pBmp = m_pTheme->getBitmapById(id); \
140 msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
146 // Macro to get the parent box of a control, given the panel ID
147 #define GET_BOX( pRect, id, pLayout ) \
149 pRect = &pLayout->getRect(); \
152 const Position *pParent = \
153 m_pTheme->getPositionById( rData.m_panelId ); \
154 if( pParent == NULL ) \
156 msg_Err( getIntf(), "parent panel could not be found: %s", \
157 rData.m_panelId.c_str() ); \
164 void Builder::addTheme( const BuilderData::Theme &rData )
166 WindowManager &rManager = m_pTheme->getWindowManager();
167 rManager.setMagnetValue( rData.m_magnet );
168 rManager.setAlphaValue( rData.m_alpha );
169 rManager.setMoveAlphaValue( rData.m_moveAlpha );
170 GenericFont *pFont = getFont( rData.m_tooltipfont );
173 rManager.createTooltip( *pFont );
177 msg_Warn( getIntf(), "invalid tooltip font: %s",
178 rData.m_tooltipfont.c_str() );
183 void Builder::addIniFile( const BuilderData::IniFile &rData )
185 // Parse the INI file
186 string full_path = getFilePath( rData.m_file );
187 if( !full_path.size() )
190 IniFile iniFile( getIntf(), rData.m_id, full_path );
195 void Builder::addBitmap( const BuilderData::Bitmap &rData )
197 string full_path = getFilePath( rData.m_fileName );
198 if( !full_path.size() )
201 GenericBitmap *pBmp =
202 new FileBitmap( getIntf(), m_pImageHandler,
203 full_path, rData.m_alphaColor,
204 rData.m_nbFrames, rData.m_fps, rData.m_nbLoops );
205 if( !pBmp->getData() )
211 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
215 void Builder::addSubBitmap( const BuilderData::SubBitmap &rData )
217 if( m_pTheme->m_bitmaps.find( rData.m_id ) != m_pTheme->m_bitmaps.end() )
219 msg_Dbg( getIntf(), "bitmap %s already exists", rData.m_id.c_str() );
223 // Get the parent bitmap
224 GenericBitmap *pParentBmp = NULL;
225 GET_BMP( pParentBmp, rData.m_parent );
227 // Copy a region of the parent bitmap to the new one
229 new BitmapImpl( getIntf(), rData.m_width, rData.m_height,
230 rData.m_nbFrames, rData.m_fps, rData.m_nbLoops );
231 bool res = pBmp->drawBitmap( *pParentBmp, rData.m_x, rData.m_y, 0, 0,
232 rData.m_width, rData.m_height );
235 // Invalid sub-bitmap
237 msg_Warn( getIntf(), "sub-bitmap %s ignored", rData.m_id.c_str() );
240 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
244 void Builder::addBitmapFont( const BuilderData::BitmapFont &rData )
246 if( m_pTheme->m_fonts.find( rData.m_id ) != m_pTheme->m_fonts.end() )
248 msg_Dbg( getIntf(), "font %s already exists", rData.m_id.c_str() );
252 string full_path = getFilePath( rData.m_file );
253 if( !full_path.size() )
256 GenericBitmap *pBmp =
257 new FileBitmap( getIntf(), m_pImageHandler, full_path, 0 );
258 if( !pBmp->getData() )
265 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
267 GenericFont *pFont = new BitmapFont( getIntf(), *pBmp, rData.m_type );
270 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
279 void Builder::addFont( const BuilderData::Font &rData )
281 string full_path = getFilePath( rData.m_fontFile );
282 if( !full_path.size() )
285 // Try to load the font from the theme directory
286 GenericFont *pFont = new FT2Font( getIntf(), full_path, rData.m_size );
289 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
295 // Font not found; try in the resource path
296 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
297 const list<string> &resPath = pOSFactory->getResourcePath();
298 const string &sep = pOSFactory->getDirSeparator();
300 list<string>::const_iterator it;
301 for( it = resPath.begin(); it != resPath.end(); ++it )
303 string path = (*it) + sep + "fonts" + sep + rData.m_fontFile;
304 pFont = new FT2Font( getIntf(), path, rData.m_size );
307 // Font loaded successfully
308 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
320 void Builder::addPopupMenu( const BuilderData::PopupMenu &rData )
322 Popup *pPopup = new Popup( getIntf(), m_pTheme->getWindowManager() );
324 m_pTheme->m_popups[rData.m_id] = PopupPtr( pPopup );
328 void Builder::addMenuItem( const BuilderData::MenuItem &rData )
330 Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
333 msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
337 CmdGeneric *pCommand = parseAction( rData.m_action );
338 if( pCommand == NULL )
340 msg_Err( getIntf(), "invalid action: %s", rData.m_action.c_str() );
344 pPopup->addItem( rData.m_label, *pCommand, rData.m_pos );
348 void Builder::addMenuSeparator( const BuilderData::MenuSeparator &rData )
350 Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
353 msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
357 pPopup->addSeparator( rData.m_pos );
361 void Builder::addWindow( const BuilderData::Window &rData )
364 if( rData.m_id == "fullscreenController" )
366 pWin = new FscWindow( getIntf(), rData.m_xPos, rData.m_yPos,
367 m_pTheme->getWindowManager(),
368 rData.m_dragDrop, rData.m_playOnDrop,
373 pWin = new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
374 m_pTheme->getWindowManager(),
375 rData.m_dragDrop, rData.m_playOnDrop,
378 m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
382 void Builder::addLayout( const BuilderData::Layout &rData )
384 TopWindow *pWin = m_pTheme->getWindowById( rData.m_windowId );
387 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
391 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
392 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
393 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
395 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
397 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
399 minWidth, maxWidth, minHeight,
401 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
403 // Attach the layout to its window
404 m_pTheme->getWindowManager().addLayout( *pWin, *pLayout );
408 void Builder::addAnchor( const BuilderData::Anchor &rData )
410 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
411 if( pLayout == NULL )
413 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
417 Bezier *pCurve = getPoints( rData.m_points.c_str() );
420 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
421 rData.m_points.c_str() );
424 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
426 // Compute the position of the anchor
427 const Position pos = makePosition( rData.m_leftTop, rData.m_leftTop,
428 rData.m_xPos, rData.m_yPos,
431 pLayout->getRect() );
433 Anchor *pAnc = new Anchor( getIntf(), pos, rData.m_range, rData.m_priority,
435 pLayout->addAnchor( pAnc );
439 void Builder::addButton( const BuilderData::Button &rData )
441 // Get the bitmaps of the button
442 GenericBitmap *pBmpUp = NULL;
443 GET_BMP( pBmpUp, rData.m_upId );
445 GenericBitmap *pBmpDown = pBmpUp;
446 GET_BMP( pBmpDown, rData.m_downId );
448 GenericBitmap *pBmpOver = pBmpUp;
449 GET_BMP( pBmpOver, rData.m_overId );
451 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
452 if( pLayout == NULL )
454 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
458 CmdGeneric *pCommand = parseAction( rData.m_actionId );
459 if( pCommand == NULL )
461 msg_Err( getIntf(), "invalid action: %s", rData.m_actionId.c_str() );
465 // Get the visibility variable
466 // XXX check when it is null
467 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
468 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
470 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
471 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
472 UString( getIntf(), rData.m_help.c_str() ), pVisible );
473 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
475 // Compute the position of the control
476 // XXX (we suppose all the images have the same size...)
477 const GenericRect *pRect;
478 GET_BOX( pRect, rData.m_panelId , pLayout);
479 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
480 rData.m_xPos, rData.m_yPos,
482 pBmpUp->getHeight() / pBmpUp->getNbFrames(),
483 *pRect, rData.m_xKeepRatio, rData.m_yKeepRatio );
485 pLayout->addControl( pButton, pos, rData.m_layer );
489 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
491 // Get the bitmaps of the checkbox
492 GenericBitmap *pBmpUp1 = NULL;
493 GET_BMP( pBmpUp1, rData.m_up1Id );
495 GenericBitmap *pBmpDown1 = pBmpUp1;
496 GET_BMP( pBmpDown1, rData.m_down1Id );
498 GenericBitmap *pBmpOver1 = pBmpUp1;
499 GET_BMP( pBmpOver1, rData.m_over1Id );
501 GenericBitmap *pBmpUp2 = NULL;
502 GET_BMP( pBmpUp2, rData.m_up2Id );
504 GenericBitmap *pBmpDown2 = pBmpUp2;
505 GET_BMP( pBmpDown2, rData.m_down2Id );
507 GenericBitmap *pBmpOver2 = pBmpUp2;
508 GET_BMP( pBmpOver2, rData.m_over2Id );
510 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
511 if( pLayout == NULL )
513 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
517 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
518 if( pCommand1 == NULL )
520 msg_Err( getIntf(), "invalid action: %s", rData.m_action1.c_str() );
524 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
525 if( pCommand2 == NULL )
527 msg_Err( getIntf(), "invalid action: %s", rData.m_action2.c_str() );
531 // Get the state variable
532 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
533 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
536 // TODO: default state
540 // Get the visibility variable
541 // XXX check when it is null
542 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
544 // Create the control
545 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
546 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
547 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
548 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
549 UString( getIntf(), rData.m_help.c_str() ), pVisible );
550 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
552 // Compute the position of the control
553 // XXX (we suppose all the images have the same size...)
554 const GenericRect *pRect;
555 GET_BOX( pRect, rData.m_panelId , pLayout);
556 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
557 rData.m_xPos, rData.m_yPos,
559 pBmpUp1->getHeight() / pBmpUp1->getNbFrames(),
560 *pRect, rData.m_xKeepRatio, rData.m_yKeepRatio );
562 pLayout->addControl( pCheckbox, pos, rData.m_layer );
566 void Builder::addImage( const BuilderData::Image &rData )
568 GenericBitmap *pBmp = NULL;
569 GET_BMP( pBmp, rData.m_bmpId );
571 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
572 if( pLayout == NULL )
574 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
578 TopWindow *pWindow = m_pTheme->getWindowById( rData.m_windowId );
579 if( pWindow == NULL )
581 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
585 CmdGeneric *pCommand = parseAction( rData.m_action2Id );
586 if( pCommand == NULL )
588 msg_Err( getIntf(), "invalid action: %s", rData.m_action2Id.c_str() );
592 // Get the visibility variable
593 // XXX check when it is null
594 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
595 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
597 CtrlImage::resize_t resizeMethod =
598 rData.m_resize == "scale" ? CtrlImage::kScale :
599 rData.m_resize == "mosaic" ? CtrlImage::kMosaic :
600 CtrlImage::kScaleAndRatioPreserved;
601 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, *pCommand,
602 resizeMethod, UString( getIntf(), rData.m_help.c_str() ), pVisible,
604 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
606 // Compute the position of the control
607 const GenericRect *pRect;
608 int width = (rData.m_width > 0) ? rData.m_width : pBmp->getWidth();
609 int height = (rData.m_height > 0) ? rData.m_height : pBmp->getHeight();
610 GET_BOX( pRect, rData.m_panelId , pLayout);
611 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
612 rData.m_xPos, rData.m_yPos,
614 *pRect, rData.m_xKeepRatio,
615 rData.m_yKeepRatio );
617 if( rData.m_actionId == "move" )
619 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
620 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
622 m_pTheme->m_controls[rData.m_id + "_move"] = CtrlGenericPtr( pMove );
623 pLayout->addControl( pMove, pos, rData.m_layer );
625 else if( rData.m_actionId == "resizeS" )
627 CtrlResize *pResize = new CtrlResize( getIntf(),
628 m_pTheme->getWindowManager(), *pImage, *pLayout,
629 UString( getIntf(), rData.m_help.c_str() ), pVisible,
630 WindowManager::kResizeS );
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 == "resizeE" )
636 CtrlResize *pResize = new CtrlResize( getIntf(),
637 m_pTheme->getWindowManager(), *pImage, *pLayout,
638 UString( getIntf(), rData.m_help.c_str() ), pVisible,
639 WindowManager::kResizeE );
640 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
641 pLayout->addControl( pResize, pos, rData.m_layer );
643 else if( rData.m_actionId == "resizeSE" )
645 CtrlResize *pResize = new CtrlResize( getIntf(),
646 m_pTheme->getWindowManager(), *pImage, *pLayout,
647 UString( getIntf(), rData.m_help.c_str() ), pVisible,
648 WindowManager::kResizeSE );
649 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
650 pLayout->addControl( pResize, pos, rData.m_layer );
654 pLayout->addControl( pImage, pos, rData.m_layer );
659 void Builder::addPanel( const BuilderData::Panel &rData )
661 // This method makes the assumption that the Panels are created in the
662 // order of the XML, because each child Panel expects its parent Panel
663 // in order to be fully created
665 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
666 if( pLayout == NULL )
668 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
672 const GenericRect *pRect;
673 GET_BOX( pRect, rData.m_panelId , pLayout);
675 new Position( makePosition( rData.m_leftTop, rData.m_rightBottom,
676 rData.m_xPos, rData.m_yPos,
677 rData.m_width, rData.m_height,
678 *pRect, rData.m_xKeepRatio,
679 rData.m_yKeepRatio ) );
680 m_pTheme->m_positions[rData.m_id] = PositionPtr( pPos );
684 void Builder::addText( const BuilderData::Text &rData )
686 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
687 if( pLayout == NULL )
689 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
693 GenericFont *pFont = getFont( rData.m_fontId );
696 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
700 // Convert the scrolling mode
701 CtrlText::Scrolling_t scrolling;
702 if( rData.m_scrolling == "auto" )
703 scrolling = CtrlText::kAutomatic;
704 else if( rData.m_scrolling == "manual" )
705 scrolling = CtrlText::kManual;
706 else if( rData.m_scrolling == "none" )
707 scrolling = CtrlText::kNone;
710 msg_Err( getIntf(), "invalid scrolling mode: %s",
711 rData.m_scrolling.c_str() );
715 // Convert the alignment
716 CtrlText::Align_t alignment;
717 if( rData.m_alignment == "left" )
718 alignment = CtrlText::kLeft;
719 else if( rData.m_alignment == "center" || rData.m_alignment == "centre" )
720 alignment = CtrlText::kCenter;
721 else if( rData.m_alignment == "right" )
722 alignment = CtrlText::kRight;
725 msg_Err( getIntf(), "invalid alignment: %s",
726 rData.m_alignment.c_str() );
730 // Create a text variable
731 VarText *pVar = new VarText( getIntf() );
732 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
734 // Set the text of the control
735 UString msg( getIntf(), rData.m_text.c_str() );
738 // Get the visibility variable
739 // XXX check when it is null
740 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
741 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
742 VarBool *pFocus = pInterpreter->getVarBool( rData.m_focus, m_pTheme );
744 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
745 UString( getIntf(), rData.m_help.c_str() ), rData.m_color,
746 pVisible, pFocus, scrolling, alignment );
747 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
749 int height = pFont->getSize();
751 // Compute the position of the control
752 const GenericRect *pRect;
753 GET_BOX( pRect, rData.m_panelId , pLayout);
754 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
755 rData.m_xPos, rData.m_yPos,
756 rData.m_width, height, *pRect,
757 rData.m_xKeepRatio, rData.m_yKeepRatio );
759 pLayout->addControl( pText, pos, rData.m_layer );
764 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
766 // Get the bitmaps of the slider
767 GenericBitmap *pSeq = NULL;
768 GET_BMP( pSeq, rData.m_sequence );
770 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
771 if( pLayout == NULL )
773 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
777 // Get the variable associated to the slider
778 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
779 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
782 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
786 // Get the visibility variable
787 // XXX check when it is null
788 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
790 // Create the control
791 CtrlRadialSlider *pRadial =
792 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
793 rData.m_minAngle, rData.m_maxAngle,
794 UString( getIntf(), rData.m_help.c_str() ),
796 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
798 // XXX: resizing is not supported
799 // Compute the position of the control
800 const GenericRect *pRect;
801 GET_BOX( pRect, rData.m_panelId , pLayout);
802 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
803 rData.m_xPos, rData.m_yPos,
805 pSeq->getHeight() / rData.m_nbImages,
807 rData.m_xKeepRatio, rData.m_yKeepRatio );
809 pLayout->addControl( pRadial, pos, rData.m_layer );
813 void Builder::addSlider( const BuilderData::Slider &rData )
815 // Add the background first, so that we will still have something almost
816 // functional if the cursor cannot be created properly (this happens for
817 // some winamp2 skins, where the images of the cursor are not always
820 // Get the bitmaps of the background
821 GenericBitmap *pBgImage = NULL;
822 if( rData.m_imageId != "none" )
824 GET_BMP( pBgImage, rData.m_imageId );
827 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
828 if( pLayout == NULL )
830 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
834 Bezier *pCurve = getPoints( rData.m_points.c_str() );
837 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
838 rData.m_points.c_str() );
841 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
843 // Get the visibility variable
844 // XXX check when it is null
845 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
846 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
848 // Get the variable associated to the slider
849 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
852 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
856 // Create the background control
857 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(),
858 *pCurve, *pVar, rData.m_thickness, pBgImage, rData.m_nbHoriz,
859 rData.m_nbVert, rData.m_padHoriz, rData.m_padVert,
860 pVisible, UString( getIntf(), rData.m_help.c_str() ) );
861 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
863 // Compute the position of the control
864 const GenericRect *pRect;
865 GET_BOX( pRect, rData.m_panelId , pLayout);
866 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
867 rData.m_xPos, rData.m_yPos,
868 pCurve->getWidth(), pCurve->getHeight(),
870 rData.m_xKeepRatio, rData.m_yKeepRatio );
872 pLayout->addControl( pBackground, pos, rData.m_layer );
874 // Get the bitmaps of the cursor
875 GenericBitmap *pBmpUp = NULL;
876 GET_BMP( pBmpUp, rData.m_upId );
878 GenericBitmap *pBmpDown = pBmpUp;
879 GET_BMP( pBmpDown, rData.m_downId );
881 GenericBitmap *pBmpOver = pBmpUp;
882 GET_BMP( pBmpOver, rData.m_overId );
884 // Create the cursor control
885 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
886 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
887 UString( getIntf(), rData.m_tooltip.c_str() ),
888 UString( getIntf(), rData.m_help.c_str() ) );
889 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
891 pLayout->addControl( pCursor, pos, rData.m_layer );
893 // Associate the cursor to the background
894 pBackground->associateCursor( *pCursor );
898 void Builder::addList( const BuilderData::List &rData )
900 // Get the background bitmap, if any
901 GenericBitmap *pBgBmp = NULL;
902 GET_BMP( pBgBmp, rData.m_bgImageId );
904 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
905 if( pLayout == NULL )
907 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
911 GenericFont *pFont = getFont( rData.m_fontId );
914 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
918 // Get the list variable
919 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
920 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
923 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
927 // Get the visibility variable
928 // XXX check when it is null
929 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
931 // Get the color values
932 uint32_t fgColor = getColor( rData.m_fgColor );
933 uint32_t playColor = getColor( rData.m_playColor );
934 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
935 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
936 uint32_t selColor = getColor( rData.m_selColor );
938 // Create the list control
939 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
940 fgColor, playColor, bgColor1, bgColor2, selColor,
941 UString( getIntf(), rData.m_help.c_str() ), pVisible );
942 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
944 // Compute the position of the control
945 const GenericRect *pRect;
946 GET_BOX( pRect, rData.m_panelId , pLayout);
947 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
948 rData.m_xPos, rData.m_yPos,
949 rData.m_width, rData.m_height,
951 rData.m_xKeepRatio, rData.m_yKeepRatio );
953 pLayout->addControl( pList, pos, rData.m_layer );
956 void Builder::addTree( const BuilderData::Tree &rData )
958 // Get the bitmaps, if any
959 GenericBitmap *pBgBmp = NULL;
960 GenericBitmap *pItemBmp = NULL;
961 GenericBitmap *pOpenBmp = NULL;
962 GenericBitmap *pClosedBmp = NULL;
963 GET_BMP( pBgBmp, rData.m_bgImageId );
964 GET_BMP( pItemBmp, rData.m_itemImageId );
965 GET_BMP( pOpenBmp, rData.m_openImageId );
966 GET_BMP( pClosedBmp, rData.m_closedImageId );
968 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
969 if( pLayout == NULL )
971 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
975 GenericFont *pFont = getFont( rData.m_fontId );
978 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
982 // Get the list variable
983 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
984 VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
987 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
991 // Get the visibility variable
992 // XXX check when it is null
993 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
994 VarBool *pFlat = pInterpreter->getVarBool( rData.m_flat, m_pTheme );
996 // Get the color values
997 uint32_t fgColor = getColor( rData.m_fgColor );
998 uint32_t playColor = getColor( rData.m_playColor );
999 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
1000 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
1001 uint32_t selColor = getColor( rData.m_selColor );
1003 // Create the list control
1004 CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
1005 pItemBmp, pOpenBmp, pClosedBmp,
1006 fgColor, playColor, bgColor1, bgColor2, selColor,
1007 UString( getIntf(), rData.m_help.c_str() ), pVisible, pFlat );
1008 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
1010 // Compute the position of the control
1011 const GenericRect *pRect;
1012 GET_BOX( pRect, rData.m_panelId , pLayout);
1013 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
1014 rData.m_xPos, rData.m_yPos,
1015 rData.m_width, rData.m_height,
1017 rData.m_xKeepRatio, rData.m_yKeepRatio );
1019 pLayout->addControl( pTree, pos, rData.m_layer );
1022 void Builder::addVideo( const BuilderData::Video &rData )
1024 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
1025 if( pLayout == NULL )
1027 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
1031 BuilderData::Video Data = rData;
1032 if( Data.m_autoResize )
1034 // force autoresize to false if the control is not able to
1035 // freely resize within its container
1036 if( Data.m_xKeepRatio || Data.m_yKeepRatio ||
1037 !( Data.m_leftTop == "lefttop" &&
1038 Data.m_rightBottom == "rightbottom" ) )
1041 "video: resize policy and autoresize are not compatible" );
1042 Data.m_autoResize = false;
1046 // Get the visibility variable
1047 // XXX check when it is null
1048 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1049 VarBool *pVisible = pInterpreter->getVarBool( Data.m_visible, m_pTheme );
1051 CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
1052 Data.m_autoResize, UString( getIntf(), Data.m_help.c_str() ),
1054 m_pTheme->m_controls[Data.m_id] = CtrlGenericPtr( pVideo );
1056 // Compute the position of the control
1057 const GenericRect *pRect;
1058 GET_BOX( pRect, rData.m_panelId , pLayout);
1059 const Position pos = makePosition( Data.m_leftTop, Data.m_rightBottom,
1060 Data.m_xPos, Data.m_yPos,
1061 Data.m_width, Data.m_height,
1063 Data.m_xKeepRatio, Data.m_yKeepRatio );
1065 pLayout->addControl( pVideo, pos, Data.m_layer );
1069 const Position Builder::makePosition( const string &rLeftTop,
1070 const string &rRightBottom,
1071 int xPos, int yPos, int width,
1072 int height, const GenericRect &rRect,
1073 bool xKeepRatio, bool yKeepRatio ) const
1075 int left = 0, top = 0, right = 0, bottom = 0;
1076 Position::Ref_t refLeftTop = Position::kLeftTop;
1077 Position::Ref_t refRightBottom = Position::kLeftTop;
1079 int boxWidth = rRect.getWidth();
1080 int boxHeight = rRect.getHeight();
1082 // Position of the left top corner
1083 if( rLeftTop == "lefttop" )
1087 refLeftTop = Position::kLeftTop;
1089 else if( rLeftTop == "righttop" )
1091 left = xPos - boxWidth + 1;
1093 refLeftTop = Position::kRightTop;
1095 else if( rLeftTop == "leftbottom" )
1098 top = yPos - boxHeight + 1;
1099 refLeftTop = Position::kLeftBottom;
1101 else if( rLeftTop == "rightbottom" )
1103 left = xPos - boxWidth + 1;
1104 top = yPos - boxHeight + 1;
1105 refLeftTop = Position::kRightBottom;
1108 // Position of the right bottom corner
1109 if( rRightBottom == "lefttop" )
1111 right = xPos + width - 1;
1112 bottom = yPos + height - 1;
1113 refRightBottom = Position::kLeftTop;
1115 else if( rRightBottom == "righttop" )
1117 right = xPos + width - boxWidth;
1118 bottom = yPos + height - 1;
1119 refRightBottom = Position::kRightTop;
1121 else if( rRightBottom == "leftbottom" )
1123 right = xPos + width - 1;
1124 bottom = yPos + height - boxHeight;
1125 refRightBottom = Position::kLeftBottom;
1127 else if( rRightBottom == "rightbottom" )
1129 right = xPos + width - boxWidth;
1130 bottom = yPos + height - boxHeight;
1131 refRightBottom = Position::kRightBottom;
1134 // In "keep ratio" mode, overwrite the previously computed values with the
1136 // XXX: this coupling between makePosition and the Position class should
1141 right = xPos + width;
1146 bottom = yPos + height;
1149 return Position( left, top, right, bottom, rRect, refLeftTop,
1150 refRightBottom, xKeepRatio, yKeepRatio );
1154 GenericFont *Builder::getFont( const string &fontId )
1156 GenericFont *pFont = m_pTheme->getFontById(fontId);
1157 if( !pFont && fontId == "defaultfont" )
1159 // Get the resource path and try to load the default font
1160 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
1161 const list<string> &resPath = pOSFactory->getResourcePath();
1162 const string &sep = pOSFactory->getDirSeparator();
1164 list<string>::const_iterator it;
1165 for( it = resPath.begin(); it != resPath.end(); ++it )
1167 string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
1168 pFont = new FT2Font( getIntf(), path, 12 );
1171 // Font loaded successfully
1172 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
1183 msg_Err( getIntf(), "failed to open the default font" );
1190 string Builder::getFilePath( const string &rFileName ) const
1192 OSFactory *pFactory = OSFactory::instance( getIntf() );
1193 const string &sep = pFactory->getDirSeparator();
1195 string file = rFileName;
1196 if( file.find( "\\" ) != string::npos )
1198 // For skins to be valid on both Linux and Win32,
1199 // slash should be used as path separator for both OSs.
1200 msg_Warn( getIntf(), "use of '/' is preferred to '\\' for paths" );
1202 while( ( pos = file.find( "\\" ) ) != string::npos )
1208 while( ( pos = file.find( "/" ) ) != string::npos )
1209 file.replace( pos, 1, sep );
1212 string full_path = m_path + sep + sFromLocale( file );
1214 // check that the file exists and can be read
1215 if( ifstream( full_path.c_str() ).fail() )
1217 msg_Err( getIntf(), "missing file: %s", file.c_str() );
1226 Bezier *Builder::getPoints( const char *pTag ) const
1228 vector<float> xBez, yBez;
1232 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
1237 xBez.push_back( x );
1238 yBez.push_back( y );
1244 if( *(pTag++) != ',' )
1250 // Create the Bezier curve
1251 return new Bezier( getIntf(), xBez, yBez );
1255 uint32_t Builder::getColor( const string &rVal ) const
1257 // Check it the value is a registered constant
1258 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1259 string val = pInterpreter->getConstant( rVal );
1261 // Convert to an int value
1262 return SkinParser::convertColor( val.c_str() );