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() ); \
145 // macro to check bitmap size consistency for button and checkbox
146 #define CHECK_BMP( pBmp, pBmpRef, id) \
147 if( pBmp != pBmpRef ) \
149 int w_ref = pBmpRef->getWidth(); \
150 int h_ref = pBmpRef->getHeight() / pBmpRef->getNbFrames(); \
151 int w = pBmp->getWidth(); \
152 int h = pBmp->getHeight() / pBmp->getNbFrames(); \
153 if( w != w_ref || h != h_ref ) \
154 msg_Err( getIntf(), "pls, check bitmap sizes for id: %s", id.c_str() ); \
157 // macro to check resize policy of button and checkbox
158 #define CHECK_RESIZE_POLICY( lefttop, rightbottom, xkeepratio, ykeepratio, id )\
159 if( (!xkeepratio && lefttop != rightbottom) || \
160 (!ykeepratio && lefttop != rightbottom) ) \
162 msg_Err( getIntf(), "pls, check resize policy for id: %s", \
164 rightbottom = lefttop; \
167 // Macro to get the parent box of a control, given the panel ID
168 #define GET_BOX( pRect, id, pLayout ) \
170 pRect = &pLayout->getRect(); \
173 const Position *pParent = \
174 m_pTheme->getPositionById( rData.m_panelId ); \
175 if( pParent == NULL ) \
177 msg_Err( getIntf(), "parent panel could not be found: %s", \
178 rData.m_panelId.c_str() ); \
185 void Builder::addTheme( const BuilderData::Theme &rData )
187 WindowManager &rManager = m_pTheme->getWindowManager();
188 rManager.setMagnetValue( rData.m_magnet );
189 rManager.setAlphaValue( rData.m_alpha );
190 rManager.setMoveAlphaValue( rData.m_moveAlpha );
191 GenericFont *pFont = getFont( rData.m_tooltipfont );
194 rManager.createTooltip( *pFont );
198 msg_Warn( getIntf(), "invalid tooltip font: %s",
199 rData.m_tooltipfont.c_str() );
204 void Builder::addIniFile( const BuilderData::IniFile &rData )
206 // Parse the INI file
207 string full_path = getFilePath( rData.m_file );
208 if( !full_path.size() )
211 IniFile iniFile( getIntf(), rData.m_id, full_path );
216 void Builder::addBitmap( const BuilderData::Bitmap &rData )
218 string full_path = getFilePath( rData.m_fileName );
219 if( !full_path.size() )
222 GenericBitmap *pBmp =
223 new FileBitmap( getIntf(), m_pImageHandler,
224 full_path, rData.m_alphaColor,
225 rData.m_nbFrames, rData.m_fps, rData.m_nbLoops );
226 if( !pBmp->getData() )
232 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
236 void Builder::addSubBitmap( const BuilderData::SubBitmap &rData )
238 if( m_pTheme->m_bitmaps.find( rData.m_id ) != m_pTheme->m_bitmaps.end() )
240 msg_Dbg( getIntf(), "bitmap %s already exists", rData.m_id.c_str() );
244 // Get the parent bitmap
245 GenericBitmap *pParentBmp = NULL;
246 GET_BMP( pParentBmp, rData.m_parent );
248 // Copy a region of the parent bitmap to the new one
250 new BitmapImpl( getIntf(), rData.m_width, rData.m_height,
251 rData.m_nbFrames, rData.m_fps, rData.m_nbLoops );
252 bool res = pBmp->drawBitmap( *pParentBmp, rData.m_x, rData.m_y, 0, 0,
253 rData.m_width, rData.m_height );
256 // Invalid sub-bitmap
258 msg_Warn( getIntf(), "sub-bitmap %s ignored", rData.m_id.c_str() );
261 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
265 void Builder::addBitmapFont( const BuilderData::BitmapFont &rData )
267 if( m_pTheme->m_fonts.find( rData.m_id ) != m_pTheme->m_fonts.end() )
269 msg_Dbg( getIntf(), "font %s already exists", rData.m_id.c_str() );
273 string full_path = getFilePath( rData.m_file );
274 if( !full_path.size() )
277 GenericBitmap *pBmp =
278 new FileBitmap( getIntf(), m_pImageHandler, full_path, 0 );
279 if( !pBmp->getData() )
286 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
288 GenericFont *pFont = new BitmapFont( getIntf(), *pBmp, rData.m_type );
291 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
300 void Builder::addFont( const BuilderData::Font &rData )
302 // Try to load the font from the theme directory
303 string full_path = getFilePath( rData.m_fontFile );
304 if( full_path.size() )
306 GenericFont *pFont = new FT2Font( getIntf(), full_path, rData.m_size );
309 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
315 // Font not found; try in the resource path
316 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
317 const list<string> &resPath = pOSFactory->getResourcePath();
318 const string &sep = pOSFactory->getDirSeparator();
320 list<string>::const_iterator it;
321 for( it = resPath.begin(); it != resPath.end(); ++it )
323 string path = (*it) + sep + "fonts" + sep + rData.m_fontFile;
324 GenericFont *pFont = new FT2Font( getIntf(), path, rData.m_size );
327 // Font loaded successfully
328 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
336 void Builder::addPopupMenu( const BuilderData::PopupMenu &rData )
338 Popup *pPopup = new Popup( getIntf(), m_pTheme->getWindowManager() );
340 m_pTheme->m_popups[rData.m_id] = PopupPtr( pPopup );
344 void Builder::addMenuItem( const BuilderData::MenuItem &rData )
346 Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
349 msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
353 CmdGeneric *pCommand = parseAction( rData.m_action );
354 if( pCommand == NULL )
356 msg_Err( getIntf(), "invalid action: %s", rData.m_action.c_str() );
360 pPopup->addItem( rData.m_label, *pCommand, rData.m_pos );
364 void Builder::addMenuSeparator( const BuilderData::MenuSeparator &rData )
366 Popup *pPopup = m_pTheme->getPopupById( rData.m_popupId );
369 msg_Err( getIntf(), "unknown popup id: %s", rData.m_popupId.c_str() );
373 pPopup->addSeparator( rData.m_pos );
377 void Builder::addWindow( const BuilderData::Window &rData )
380 if( rData.m_id == "fullscreenController" )
382 pWin = new FscWindow( getIntf(), rData.m_xPos, rData.m_yPos,
383 m_pTheme->getWindowManager(),
384 rData.m_dragDrop, rData.m_playOnDrop,
389 pWin = new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
390 m_pTheme->getWindowManager(),
391 rData.m_dragDrop, rData.m_playOnDrop,
394 m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
398 void Builder::addLayout( const BuilderData::Layout &rData )
400 TopWindow *pWin = m_pTheme->getWindowById( rData.m_windowId );
403 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
407 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
408 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
409 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
411 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
413 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
415 minWidth, maxWidth, minHeight,
417 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
419 // Attach the layout to its window
420 m_pTheme->getWindowManager().addLayout( *pWin, *pLayout );
424 void Builder::addAnchor( const BuilderData::Anchor &rData )
426 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
427 if( pLayout == NULL )
429 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
433 Bezier *pCurve = getPoints( rData.m_points.c_str() );
436 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
437 rData.m_points.c_str() );
440 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
442 // Compute the position of the anchor
443 const Position pos = makePosition( rData.m_leftTop, rData.m_leftTop,
444 rData.m_xPos, rData.m_yPos,
447 pLayout->getRect() );
449 Anchor *pAnc = new Anchor( getIntf(), pos, rData.m_range, rData.m_priority,
451 pLayout->addAnchor( pAnc );
455 void Builder::addButton( const BuilderData::Button &rData )
457 // Get the bitmaps of the button
458 GenericBitmap *pBmpUp = NULL;
459 GET_BMP( pBmpUp, rData.m_upId );
461 GenericBitmap *pBmpDown = pBmpUp;
462 GET_BMP( pBmpDown, rData.m_downId );
464 GenericBitmap *pBmpOver = pBmpUp;
465 GET_BMP( pBmpOver, rData.m_overId );
467 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
468 if( pLayout == NULL )
470 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
474 CmdGeneric *pCommand = parseAction( rData.m_actionId );
475 if( pCommand == NULL )
477 msg_Err( getIntf(), "invalid action: %s", rData.m_actionId.c_str() );
481 // Get the visibility variable
482 // XXX check when it is null
483 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
484 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
486 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
487 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
488 UString( getIntf(), rData.m_help.c_str() ), pVisible );
489 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
491 // width and height are set up from the 'up' bitmap size
492 int width = pBmpUp->getWidth();
493 int height = pBmpUp->getHeight() / pBmpUp->getNbFrames();
494 bool xkeepratio = rData.m_xKeepRatio;
495 bool ykeepratio = rData.m_yKeepRatio;
496 string lefttop( rData.m_leftTop );
497 string rightbottom( rData.m_rightBottom );
499 // various checks to help skin developers debug their skin
500 CHECK_BMP( pBmpDown, pBmpUp, rData.m_id );
501 CHECK_BMP( pBmpOver, pBmpUp, rData.m_id );
502 CHECK_RESIZE_POLICY( lefttop, rightbottom, xkeepratio, ykeepratio,
505 // Compute the position of the control
506 // XXX (we suppose all the images have the same size...)
507 const GenericRect *pRect;
508 GET_BOX( pRect, rData.m_panelId , pLayout);
509 const Position pos = makePosition( lefttop, rightbottom,
510 rData.m_xPos, rData.m_yPos, width, height,
511 *pRect, xkeepratio, ykeepratio );
513 pLayout->addControl( pButton, pos, rData.m_layer );
517 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
519 // Get the bitmaps of the checkbox
520 GenericBitmap *pBmpUp1 = NULL;
521 GET_BMP( pBmpUp1, rData.m_up1Id );
523 GenericBitmap *pBmpDown1 = pBmpUp1;
524 GET_BMP( pBmpDown1, rData.m_down1Id );
526 GenericBitmap *pBmpOver1 = pBmpUp1;
527 GET_BMP( pBmpOver1, rData.m_over1Id );
529 GenericBitmap *pBmpUp2 = NULL;
530 GET_BMP( pBmpUp2, rData.m_up2Id );
532 GenericBitmap *pBmpDown2 = pBmpUp2;
533 GET_BMP( pBmpDown2, rData.m_down2Id );
535 GenericBitmap *pBmpOver2 = pBmpUp2;
536 GET_BMP( pBmpOver2, rData.m_over2Id );
538 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
539 if( pLayout == NULL )
541 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
545 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
546 if( pCommand1 == NULL )
548 msg_Err( getIntf(), "invalid action: %s", rData.m_action1.c_str() );
552 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
553 if( pCommand2 == NULL )
555 msg_Err( getIntf(), "invalid action: %s", rData.m_action2.c_str() );
559 // Get the state variable
560 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
561 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
564 // TODO: default state
568 // Get the visibility variable
569 // XXX check when it is null
570 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
572 // width and height are set up from the 'up1' bitmap size
573 int width = pBmpUp1->getWidth();
574 int height = pBmpUp1->getHeight() / pBmpUp1->getNbFrames();
575 bool xkeepratio = rData.m_xKeepRatio;
576 bool ykeepratio = rData.m_yKeepRatio;
577 string lefttop( rData.m_leftTop );
578 string rightbottom( rData.m_rightBottom );
581 // various checks to help skin developers debug their skin
582 CHECK_BMP( pBmpDown1, pBmpUp1, rData.m_id );
583 CHECK_BMP( pBmpOver1, pBmpUp1, rData.m_id );
584 CHECK_BMP( pBmpUp2, pBmpUp1, rData.m_id );
585 CHECK_BMP( pBmpDown2, pBmpUp1, rData.m_id );
586 CHECK_BMP( pBmpOver2, pBmpUp1, rData.m_id );
587 CHECK_RESIZE_POLICY( lefttop, rightbottom, xkeepratio, ykeepratio,
590 // Create the control
591 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
592 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
593 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
594 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
595 UString( getIntf(), rData.m_help.c_str() ), pVisible );
596 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
598 // Compute the position of the control
599 // XXX (we suppose all the images have the same size...)
600 const GenericRect *pRect;
601 GET_BOX( pRect, rData.m_panelId , pLayout);
602 const Position pos = makePosition( lefttop, rightbottom,
603 rData.m_xPos, rData.m_yPos, width, height,
604 *pRect, xkeepratio, ykeepratio );
606 pLayout->addControl( pCheckbox, pos, rData.m_layer );
610 void Builder::addImage( const BuilderData::Image &rData )
612 GenericBitmap *pBmp = NULL;
613 GET_BMP( pBmp, rData.m_bmpId );
615 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
616 if( pLayout == NULL )
618 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
622 TopWindow *pWindow = m_pTheme->getWindowById( rData.m_windowId );
623 if( pWindow == NULL )
625 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
629 CmdGeneric *pCommand = parseAction( rData.m_action2Id );
630 if( pCommand == NULL )
632 msg_Err( getIntf(), "invalid action: %s", rData.m_action2Id.c_str() );
636 // Get the visibility variable
637 // XXX check when it is null
638 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
639 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
641 CtrlImage::resize_t resizeMethod =
642 rData.m_resize == "scale" ? CtrlImage::kScale :
643 rData.m_resize == "mosaic" ? CtrlImage::kMosaic :
644 CtrlImage::kScaleAndRatioPreserved;
645 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp, *pCommand,
646 resizeMethod, UString( getIntf(), rData.m_help.c_str() ), pVisible,
648 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
650 // Compute the position of the control
651 const GenericRect *pRect;
652 int width = (rData.m_width > 0) ? rData.m_width : pBmp->getWidth();
653 int height = (rData.m_height > 0) ? rData.m_height : pBmp->getHeight();
654 GET_BOX( pRect, rData.m_panelId , pLayout);
655 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
656 rData.m_xPos, rData.m_yPos,
658 *pRect, rData.m_xKeepRatio,
659 rData.m_yKeepRatio );
661 if( rData.m_actionId == "move" )
663 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
664 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
666 m_pTheme->m_controls[rData.m_id + "_move"] = CtrlGenericPtr( pMove );
667 pLayout->addControl( pMove, pos, rData.m_layer );
669 else if( rData.m_actionId == "resizeS" )
671 CtrlResize *pResize = new CtrlResize( getIntf(),
672 m_pTheme->getWindowManager(), *pImage, *pLayout,
673 UString( getIntf(), rData.m_help.c_str() ), pVisible,
674 WindowManager::kResizeS );
675 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
676 pLayout->addControl( pResize, pos, rData.m_layer );
678 else if( rData.m_actionId == "resizeE" )
680 CtrlResize *pResize = new CtrlResize( getIntf(),
681 m_pTheme->getWindowManager(), *pImage, *pLayout,
682 UString( getIntf(), rData.m_help.c_str() ), pVisible,
683 WindowManager::kResizeE );
684 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
685 pLayout->addControl( pResize, pos, rData.m_layer );
687 else if( rData.m_actionId == "resizeSE" )
689 CtrlResize *pResize = new CtrlResize( getIntf(),
690 m_pTheme->getWindowManager(), *pImage, *pLayout,
691 UString( getIntf(), rData.m_help.c_str() ), pVisible,
692 WindowManager::kResizeSE );
693 m_pTheme->m_controls[rData.m_id + "_rsz"] = CtrlGenericPtr( pResize );
694 pLayout->addControl( pResize, pos, rData.m_layer );
698 pLayout->addControl( pImage, pos, rData.m_layer );
703 void Builder::addPanel( const BuilderData::Panel &rData )
705 // This method makes the assumption that the Panels are created in the
706 // order of the XML, because each child Panel expects its parent Panel
707 // in order to be fully created
709 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
710 if( pLayout == NULL )
712 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
716 const GenericRect *pRect;
717 GET_BOX( pRect, rData.m_panelId , pLayout);
719 new Position( makePosition( rData.m_leftTop, rData.m_rightBottom,
720 rData.m_xPos, rData.m_yPos,
721 rData.m_width, rData.m_height,
722 *pRect, rData.m_xKeepRatio,
723 rData.m_yKeepRatio ) );
724 m_pTheme->m_positions[rData.m_id] = PositionPtr( pPos );
728 void Builder::addText( const BuilderData::Text &rData )
730 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
731 if( pLayout == NULL )
733 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
737 GenericFont *pFont = getFont( rData.m_fontId );
740 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
744 // Convert the scrolling mode
745 CtrlText::Scrolling_t scrolling;
746 if( rData.m_scrolling == "auto" )
747 scrolling = CtrlText::kAutomatic;
748 else if( rData.m_scrolling == "manual" )
749 scrolling = CtrlText::kManual;
750 else if( rData.m_scrolling == "none" )
751 scrolling = CtrlText::kNone;
754 msg_Err( getIntf(), "invalid scrolling mode: %s",
755 rData.m_scrolling.c_str() );
759 // Convert the alignment
760 CtrlText::Align_t alignment;
761 if( rData.m_alignment == "left" )
762 alignment = CtrlText::kLeft;
763 else if( rData.m_alignment == "center" || rData.m_alignment == "centre" )
764 alignment = CtrlText::kCenter;
765 else if( rData.m_alignment == "right" )
766 alignment = CtrlText::kRight;
769 msg_Err( getIntf(), "invalid alignment: %s",
770 rData.m_alignment.c_str() );
774 // Create a text variable
775 VarText *pVar = new VarText( getIntf() );
776 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
778 // Set the text of the control
779 UString msg( getIntf(), rData.m_text.c_str() );
782 // Get the visibility variable
783 // XXX check when it is null
784 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
785 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
786 VarBool *pFocus = pInterpreter->getVarBool( rData.m_focus, m_pTheme );
788 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
789 UString( getIntf(), rData.m_help.c_str() ), rData.m_color,
790 pVisible, pFocus, scrolling, alignment );
791 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
793 int height = pFont->getSize();
795 // Compute the position of the control
796 const GenericRect *pRect;
797 GET_BOX( pRect, rData.m_panelId , pLayout);
798 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
799 rData.m_xPos, rData.m_yPos,
800 rData.m_width, height, *pRect,
801 rData.m_xKeepRatio, rData.m_yKeepRatio );
803 pLayout->addControl( pText, pos, rData.m_layer );
808 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
810 // Get the bitmaps of the slider
811 GenericBitmap *pSeq = NULL;
812 GET_BMP( pSeq, rData.m_sequence );
814 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
815 if( pLayout == NULL )
817 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
821 // Get the variable associated to the slider
822 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
823 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
826 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
830 // Get the visibility variable
831 // XXX check when it is null
832 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
834 // Create the control
835 CtrlRadialSlider *pRadial =
836 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
837 rData.m_minAngle, rData.m_maxAngle,
838 UString( getIntf(), rData.m_help.c_str() ),
840 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
842 // XXX: resizing is not supported
843 // Compute the position of the control
844 const GenericRect *pRect;
845 GET_BOX( pRect, rData.m_panelId , pLayout);
846 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
847 rData.m_xPos, rData.m_yPos,
849 pSeq->getHeight() / rData.m_nbImages,
851 rData.m_xKeepRatio, rData.m_yKeepRatio );
853 pLayout->addControl( pRadial, pos, rData.m_layer );
857 void Builder::addSlider( const BuilderData::Slider &rData )
859 // Add the background first, so that we will still have something almost
860 // functional if the cursor cannot be created properly (this happens for
861 // some winamp2 skins, where the images of the cursor are not always
864 // Get the bitmaps of the background
865 GenericBitmap *pBgImage = NULL;
866 GET_BMP( pBgImage, rData.m_imageId );
868 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
869 if( pLayout == NULL )
871 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
875 Bezier *pCurve = getPoints( rData.m_points.c_str() );
878 msg_Err( getIntf(), "invalid format in tag points=\"%s\"",
879 rData.m_points.c_str() );
882 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
884 // Get the visibility variable
885 // XXX check when it is null
886 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
887 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
889 // Get the variable associated to the slider
890 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
893 msg_Err( getIntf(), "unknown slider value: %s", rData.m_value.c_str() );
897 // Create the background control
898 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(),
899 *pCurve, *pVar, rData.m_thickness, pBgImage, rData.m_nbHoriz,
900 rData.m_nbVert, rData.m_padHoriz, rData.m_padVert,
901 pVisible, UString( getIntf(), rData.m_help.c_str() ) );
902 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
904 // Compute the position of the control
905 int width = (rData.m_width > 0) ? rData.m_width : pCurve->getWidth();
906 int height = (rData.m_height > 0) ? rData.m_height : pCurve->getHeight();
907 const GenericRect *pRect;
908 GET_BOX( pRect, rData.m_panelId , pLayout);
909 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
910 rData.m_xPos, rData.m_yPos,
911 width, height, *pRect,
912 rData.m_xKeepRatio, rData.m_yKeepRatio );
914 pLayout->addControl( pBackground, pos, rData.m_layer );
916 // Get the bitmaps of the cursor
917 GenericBitmap *pBmpUp = NULL;
918 GET_BMP( pBmpUp, rData.m_upId );
920 GenericBitmap *pBmpDown = pBmpUp;
921 GET_BMP( pBmpDown, rData.m_downId );
923 GenericBitmap *pBmpOver = pBmpUp;
924 GET_BMP( pBmpOver, rData.m_overId );
926 // Create the cursor control
927 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
928 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
929 UString( getIntf(), rData.m_tooltip.c_str() ),
930 UString( getIntf(), rData.m_help.c_str() ) );
931 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
933 pLayout->addControl( pCursor, pos, rData.m_layer );
935 // Associate the cursor to the background
936 pBackground->associateCursor( *pCursor );
940 void Builder::addList( const BuilderData::List &rData )
942 // Get the background bitmap, if any
943 GenericBitmap *pBgBmp = NULL;
944 GET_BMP( pBgBmp, rData.m_bgImageId );
946 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
947 if( pLayout == NULL )
949 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
953 GenericFont *pFont = getFont( rData.m_fontId );
956 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
960 // Get the list variable
961 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
962 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
965 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
969 // Get the visibility variable
970 // XXX check when it is null
971 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
973 // Get the color values
974 uint32_t fgColor = getColor( rData.m_fgColor );
975 uint32_t playColor = getColor( rData.m_playColor );
976 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
977 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
978 uint32_t selColor = getColor( rData.m_selColor );
980 // Create the list control
981 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont, pBgBmp,
982 fgColor, playColor, bgColor1, bgColor2, selColor,
983 UString( getIntf(), rData.m_help.c_str() ), pVisible );
984 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
986 // Compute the position of the control
987 const GenericRect *pRect;
988 GET_BOX( pRect, rData.m_panelId , pLayout);
989 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
990 rData.m_xPos, rData.m_yPos,
991 rData.m_width, rData.m_height,
993 rData.m_xKeepRatio, rData.m_yKeepRatio );
995 pLayout->addControl( pList, pos, rData.m_layer );
998 void Builder::addTree( const BuilderData::Tree &rData )
1000 // Get the bitmaps, if any
1001 GenericBitmap *pBgBmp = NULL;
1002 GenericBitmap *pItemBmp = NULL;
1003 GenericBitmap *pOpenBmp = NULL;
1004 GenericBitmap *pClosedBmp = NULL;
1005 GET_BMP( pBgBmp, rData.m_bgImageId );
1006 GET_BMP( pItemBmp, rData.m_itemImageId );
1007 GET_BMP( pOpenBmp, rData.m_openImageId );
1008 GET_BMP( pClosedBmp, rData.m_closedImageId );
1010 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
1011 if( pLayout == NULL )
1013 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
1017 GenericFont *pFont = getFont( rData.m_fontId );
1020 msg_Err( getIntf(), "unknown font id: %s", rData.m_fontId.c_str() );
1024 // Get the list variable
1025 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1026 VarTree *pVar = pInterpreter->getVarTree( rData.m_var, m_pTheme );
1029 msg_Err( getIntf(), "no such list variable: %s", rData.m_var.c_str() );
1033 // Get the visibility variable
1034 // XXX check when it is null
1035 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
1036 VarBool *pFlat = pInterpreter->getVarBool( rData.m_flat, m_pTheme );
1038 // Get the color values
1039 uint32_t fgColor = getColor( rData.m_fgColor );
1040 uint32_t playColor = getColor( rData.m_playColor );
1041 uint32_t bgColor1 = getColor( rData.m_bgColor1 );
1042 uint32_t bgColor2 = getColor( rData.m_bgColor2 );
1043 uint32_t selColor = getColor( rData.m_selColor );
1045 // Create the list control
1046 CtrlTree *pTree = new CtrlTree( getIntf(), *pVar, *pFont, pBgBmp,
1047 pItemBmp, pOpenBmp, pClosedBmp,
1048 fgColor, playColor, bgColor1, bgColor2, selColor,
1049 UString( getIntf(), rData.m_help.c_str() ), pVisible, pFlat );
1050 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pTree );
1052 // Compute the position of the control
1053 const GenericRect *pRect;
1054 GET_BOX( pRect, rData.m_panelId , pLayout);
1055 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
1056 rData.m_xPos, rData.m_yPos,
1057 rData.m_width, rData.m_height,
1059 rData.m_xKeepRatio, rData.m_yKeepRatio );
1061 pLayout->addControl( pTree, pos, rData.m_layer );
1064 void Builder::addVideo( const BuilderData::Video &rData )
1066 GenericLayout *pLayout = m_pTheme->getLayoutById( rData.m_layoutId );
1067 if( pLayout == NULL )
1069 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
1073 BuilderData::Video Data = rData;
1074 if( Data.m_autoResize )
1076 // force autoresize to false if the control is not able to
1077 // freely resize within its container
1078 if( Data.m_xKeepRatio || Data.m_yKeepRatio ||
1079 !( Data.m_leftTop == "lefttop" &&
1080 Data.m_rightBottom == "rightbottom" ) )
1083 "video: resize policy and autoresize are not compatible" );
1084 Data.m_autoResize = false;
1087 if( !(Data.m_width > 0 && Data.m_height > 0) )
1090 "pls, provide a valid size for the video control id: %s "
1091 "(dropping the video control)", Data.m_id.c_str() );
1095 // Get the visibility variable
1096 // XXX check when it is null
1097 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1098 VarBool *pVisible = pInterpreter->getVarBool( Data.m_visible, m_pTheme );
1100 CtrlVideo *pVideo = new CtrlVideo( getIntf(), *pLayout,
1101 Data.m_autoResize, UString( getIntf(), Data.m_help.c_str() ),
1103 m_pTheme->m_controls[Data.m_id] = CtrlGenericPtr( pVideo );
1105 // Compute the position of the control
1106 const GenericRect *pRect;
1107 GET_BOX( pRect, rData.m_panelId , pLayout);
1108 const Position pos = makePosition( Data.m_leftTop, Data.m_rightBottom,
1109 Data.m_xPos, Data.m_yPos,
1110 Data.m_width, Data.m_height,
1112 Data.m_xKeepRatio, Data.m_yKeepRatio );
1114 pLayout->addControl( pVideo, pos, Data.m_layer );
1118 const Position Builder::makePosition( const string &rLeftTop,
1119 const string &rRightBottom,
1120 int xPos, int yPos, int width,
1121 int height, const GenericRect &rRect,
1122 bool xKeepRatio, bool yKeepRatio ) const
1124 int left = 0, top = 0, right = 0, bottom = 0;
1125 Position::Ref_t refLeftTop = Position::kLeftTop;
1126 Position::Ref_t refRightBottom = Position::kLeftTop;
1128 int boxWidth = rRect.getWidth();
1129 int boxHeight = rRect.getHeight();
1131 // Position of the left top corner
1132 if( rLeftTop == "lefttop" )
1136 refLeftTop = Position::kLeftTop;
1138 else if( rLeftTop == "righttop" )
1140 left = xPos - boxWidth + 1;
1142 refLeftTop = Position::kRightTop;
1144 else if( rLeftTop == "leftbottom" )
1147 top = yPos - boxHeight + 1;
1148 refLeftTop = Position::kLeftBottom;
1150 else if( rLeftTop == "rightbottom" )
1152 left = xPos - boxWidth + 1;
1153 top = yPos - boxHeight + 1;
1154 refLeftTop = Position::kRightBottom;
1157 // Position of the right bottom corner
1158 if( rRightBottom == "lefttop" )
1160 right = xPos + width - 1;
1161 bottom = yPos + height - 1;
1162 refRightBottom = Position::kLeftTop;
1164 else if( rRightBottom == "righttop" )
1166 right = xPos + width - boxWidth;
1167 bottom = yPos + height - 1;
1168 refRightBottom = Position::kRightTop;
1170 else if( rRightBottom == "leftbottom" )
1172 right = xPos + width - 1;
1173 bottom = yPos + height - boxHeight;
1174 refRightBottom = Position::kLeftBottom;
1176 else if( rRightBottom == "rightbottom" )
1178 right = xPos + width - boxWidth;
1179 bottom = yPos + height - boxHeight;
1180 refRightBottom = Position::kRightBottom;
1183 // In "keep ratio" mode, overwrite the previously computed values with the
1185 // XXX: this coupling between makePosition and the Position class should
1190 right = xPos + width;
1195 bottom = yPos + height;
1198 return Position( left, top, right, bottom, rRect, refLeftTop,
1199 refRightBottom, xKeepRatio, yKeepRatio );
1203 GenericFont *Builder::getFont( const string &fontId )
1205 GenericFont *pFont = m_pTheme->getFontById(fontId);
1206 if( !pFont && fontId == "defaultfont" )
1208 // Get the resource path and try to load the default font
1209 OSFactory *pOSFactory = OSFactory::instance( getIntf() );
1210 const list<string> &resPath = pOSFactory->getResourcePath();
1211 const string &sep = pOSFactory->getDirSeparator();
1213 list<string>::const_iterator it;
1214 for( it = resPath.begin(); it != resPath.end(); ++it )
1216 string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
1217 pFont = new FT2Font( getIntf(), path, 12 );
1220 // Font loaded successfully
1221 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
1232 msg_Err( getIntf(), "failed to open the default font" );
1239 string Builder::getFilePath( const string &rFileName ) const
1241 OSFactory *pFactory = OSFactory::instance( getIntf() );
1242 const string &sep = pFactory->getDirSeparator();
1244 string file = rFileName;
1245 if( file.find( "\\" ) != string::npos )
1247 // For skins to be valid on both Linux and Win32,
1248 // slash should be used as path separator for both OSs.
1249 msg_Warn( getIntf(), "use of '/' is preferred to '\\' for paths" );
1250 string::size_type pos;
1251 while( ( pos = file.find( "\\" ) ) != string::npos )
1255 #if defined( _WIN32 ) || defined( __OS2__ )
1256 string::size_type pos;
1257 while( ( pos = file.find( "/" ) ) != string::npos )
1258 file.replace( pos, 1, sep );
1261 string full_path = m_path + sep + sFromLocale( file );
1263 // check that the file exists and can be read
1264 if( ifstream( full_path.c_str() ).fail() )
1266 msg_Err( getIntf(), "missing file: %s", file.c_str() );
1275 Bezier *Builder::getPoints( const char *pTag ) const
1277 vector<float> xBez, yBez;
1281 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
1286 xBez.push_back( x );
1287 yBez.push_back( y );
1293 if( *(pTag++) != ',' )
1299 // Create the Bezier curve
1300 return new Bezier( getIntf(), xBez, yBez );
1304 uint32_t Builder::getColor( const string &rVal ) const
1306 // Check it the value is a registered constant
1307 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
1308 string val = pInterpreter->getConstant( rVal );
1310 // Convert to an int value
1311 return SkinParser::convertColor( val.c_str() );