1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
26 #include "builder.hpp"
27 #include "builder_data.hpp"
28 #include "interpreter.hpp"
29 #include "../src/png_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/ft2_font.hpp"
35 #include "../src/theme.hpp"
36 #include "../controls/ctrl_button.hpp"
37 #include "../controls/ctrl_checkbox.hpp"
38 #include "../controls/ctrl_image.hpp"
39 #include "../controls/ctrl_list.hpp"
40 #include "../controls/ctrl_move.hpp"
41 #include "../controls/ctrl_resize.hpp"
42 #include "../controls/ctrl_slider.hpp"
43 #include "../controls/ctrl_radialslider.hpp"
44 #include "../controls/ctrl_text.hpp"
45 #include "../controls/ctrl_video.hpp"
46 #include "../utils/position.hpp"
47 #include "../utils/var_bool.hpp"
48 #include "../utils/var_text.hpp"
51 Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData):
52 SkinObject( pIntf ), m_rData( rData ), m_pTheme( NULL )
57 CmdGeneric *Builder::parseAction( const string &rAction )
59 return Interpreter::instance( getIntf() )->parseAction( rAction, m_pTheme );
64 #define ADD_OBJECTS( type ) \
65 list<BuilderData::type>::const_iterator it##type; \
66 for( it##type = m_rData.m_list##type.begin(); \
67 it##type != m_rData.m_list##type.end(); it##type++ ) \
69 add##type( *it##type ); \
73 Theme *Builder::build()
75 m_pTheme = new Theme( getIntf() );
76 if( m_pTheme == NULL )
81 // Create everything from the data in the XML
83 ADD_OBJECTS( Bitmap );
85 ADD_OBJECTS( Window );
86 ADD_OBJECTS( Layout );
87 ADD_OBJECTS( Anchor );
88 ADD_OBJECTS( Button );
89 ADD_OBJECTS( Checkbox );
92 ADD_OBJECTS( RadialSlider );
93 ADD_OBJECTS( Slider );
101 // Macro to get a bitmap by its ID in the builder
102 #define GET_BMP( pBmp, id ) \
105 pBmp = m_pTheme->m_bitmaps[id].get(); \
108 msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
113 void Builder::addTheme( const BuilderData::Theme &rData )
115 WindowManager &rManager = m_pTheme->getWindowManager();
116 rManager.setMagnetValue( rData.m_magnet );
117 rManager.setAlphaValue( rData.m_alpha );
118 rManager.setMoveAlphaValue( rData.m_moveAlpha );
119 GenericFont *pFont = getFont( rData.m_tooltipfont );
122 rManager.createTooltip( *pFont );
126 msg_Warn( getIntf(), "Invalid tooltip font: %s",
127 rData.m_tooltipfont.c_str() );
132 void Builder::addBitmap( const BuilderData::Bitmap &rData )
134 GenericBitmap *pBmp = new PngBitmap( getIntf(), rData.m_fileName,
135 rData.m_alphaColor );
136 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
140 void Builder::addFont( const BuilderData::Font &rData )
142 GenericFont *pFont = new FT2Font( getIntf(), rData.m_fontFile,
146 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
155 void Builder::addWindow( const BuilderData::Window &rData )
158 new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
159 m_pTheme->getWindowManager(),
160 rData.m_dragDrop, rData.m_playOnDrop );
162 m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
166 void Builder::addLayout( const BuilderData::Layout &rData )
168 TopWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
171 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
175 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
176 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
177 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
179 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
181 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
183 minWidth, maxWidth, minHeight,
185 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
187 // Attach the layout to its window
188 pWin->setActiveLayout( pLayout );
192 void Builder::addAnchor( const BuilderData::Anchor &rData )
194 TopWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
197 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
201 Bezier *pCurve = getPoints( rData.m_points.c_str() );
204 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
205 rData.m_points.c_str() );
208 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
210 Anchor *pAnc = new Anchor( getIntf(), rData.m_xPos, rData.m_yPos,
211 rData.m_range, rData.m_priority,
213 pWin->addAnchor( pAnc );
217 void Builder::addButton( const BuilderData::Button &rData )
219 // Get the bitmaps of the button
220 GenericBitmap *pBmpUp = NULL;
221 GET_BMP( pBmpUp, rData.m_upId );
223 GenericBitmap *pBmpDown = pBmpUp;
224 GET_BMP( pBmpDown, rData.m_downId );
226 GenericBitmap *pBmpOver = pBmpUp;
227 GET_BMP( pBmpOver, rData.m_overId );
229 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
230 if( pLayout == NULL )
232 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
236 CmdGeneric *pCommand = parseAction( rData.m_actionId );
237 if( pCommand == NULL )
239 msg_Err( getIntf(), "Invalid action: %s", rData.m_actionId.c_str() );
243 // Get the visibility variable
244 // XXX check when it is null
245 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
246 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
248 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
249 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
250 UString( getIntf(), rData.m_help.c_str() ), pVisible );
252 // Compute the position of the control
253 // XXX (we suppose all the images have the same size...)
254 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
255 rData.m_xPos, rData.m_yPos,
257 pBmpUp->getHeight(), *pLayout );
259 pLayout->addControl( pButton, pos, rData.m_layer );
261 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
265 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
267 // Get the bitmaps of the checkbox
268 GenericBitmap *pBmpUp1 = NULL;
269 GET_BMP( pBmpUp1, rData.m_up1Id );
271 GenericBitmap *pBmpDown1 = pBmpUp1;
272 GET_BMP( pBmpDown1, rData.m_down1Id );
274 GenericBitmap *pBmpOver1 = pBmpUp1;
275 GET_BMP( pBmpOver1, rData.m_over1Id );
277 GenericBitmap *pBmpUp2 = NULL;
278 GET_BMP( pBmpUp2, rData.m_up2Id );
280 GenericBitmap *pBmpDown2 = pBmpUp2;
281 GET_BMP( pBmpDown2, rData.m_down2Id );
283 GenericBitmap *pBmpOver2 = pBmpUp2;
284 GET_BMP( pBmpOver2, rData.m_over2Id );
286 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
287 if( pLayout == NULL )
289 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
293 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
294 if( pCommand1 == NULL )
296 msg_Err( getIntf(), "Invalid action: %s", rData.m_action1.c_str() );
300 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
301 if( pCommand2 == NULL )
303 msg_Err( getIntf(), "Invalid action: %s", rData.m_action2.c_str() );
307 // Get the state variable
308 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
309 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
312 // TODO: default state
316 // Get the visibility variable
317 // XXX check when it is null
318 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
320 // Create the control
321 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
322 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
323 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
324 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
325 UString( getIntf(), rData.m_help.c_str() ), pVisible );
327 // Compute the position of the control
328 // XXX (we suppose all the images have the same size...)
329 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
330 rData.m_xPos, rData.m_yPos,
332 pBmpUp1->getHeight(), *pLayout );
334 pLayout->addControl( pCheckbox, pos, rData.m_layer );
336 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
340 void Builder::addImage( const BuilderData::Image &rData )
342 GenericBitmap *pBmp = NULL;
343 GET_BMP( pBmp, rData.m_bmpId );
345 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
346 if( pLayout == NULL )
348 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
352 TopWindow *pWindow = m_pTheme->m_windows[rData.m_windowId].get();
353 if( pWindow == NULL )
355 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
359 // Get the visibility variable
360 // XXX check when it is null
361 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
362 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
364 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp,
365 UString( getIntf(), rData.m_help.c_str() ), pVisible );
367 // Compute the position of the control
368 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
370 rData.m_yPos, pBmp->getWidth(),
371 pBmp->getHeight(), *pLayout );
373 // XXX: test to be changed! XXX
374 if( rData.m_actionId == "move" )
376 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
377 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
379 pLayout->addControl( pMove, pos, rData.m_layer );
381 else if( rData.m_actionId == "resize" )
383 CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
384 UString( getIntf(), rData.m_help.c_str() ), NULL );
385 pLayout->addControl( pResize, pos, rData.m_layer );
389 pLayout->addControl( pImage, pos, rData.m_layer );
392 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
396 void Builder::addText( const BuilderData::Text &rData )
398 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
399 if( pLayout == NULL )
401 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
405 GenericFont *pFont = getFont( rData.m_fontId );
408 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
412 // Create a text variable
413 VarText *pVar = new VarText( getIntf() );
414 UString msg( getIntf(), rData.m_text.c_str() );
416 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
418 // Get the visibility variable
419 // XXX check when it is null
420 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
421 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
423 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
424 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible );
426 int height = pFont->getSize();
428 pLayout->addControl( pText, Position( rData.m_xPos, rData.m_yPos,
429 rData.m_xPos + rData.m_width,
430 rData.m_yPos + height, *pLayout ),
433 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
437 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
439 // Get the bitmaps of the slider
440 GenericBitmap *pSeq = NULL;
441 GET_BMP( pSeq, rData.m_sequence );
443 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
444 if( pLayout == NULL )
446 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
450 // Get the variable associated to the slider
451 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
452 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
455 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
459 // Get the visibility variable
460 // XXX check when it is null
461 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
463 // Create the control
464 CtrlRadialSlider *pRadial =
465 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
466 rData.m_minAngle, rData.m_maxAngle,
467 UString( getIntf(), rData.m_help.c_str() ),
470 // XXX: resizing is not supported
471 // Compute the position of the control
473 makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
474 rData.m_yPos, pSeq->getWidth(),
475 pSeq->getHeight() / rData.m_nbImages, *pLayout );
477 pLayout->addControl( pRadial, pos, rData.m_layer );
479 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
483 void Builder::addSlider( const BuilderData::Slider &rData )
485 // Get the bitmaps of the slider
486 GenericBitmap *pBmpUp = NULL;
487 GET_BMP( pBmpUp, rData.m_upId );
489 GenericBitmap *pBmpDown = pBmpUp;
490 GET_BMP( pBmpDown, rData.m_downId );
492 GenericBitmap *pBmpOver = pBmpUp;
493 GET_BMP( pBmpOver, rData.m_overId );
495 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
496 if( pLayout == NULL )
498 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
502 Bezier *pCurve = getPoints( rData.m_points.c_str() );
505 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
506 rData.m_points.c_str() );
509 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
511 // Get the visibility variable
512 // XXX check when it is null
513 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
514 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
516 // Get the variable associated to the slider
517 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
520 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
524 // Create the cursor and background controls
525 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
526 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
527 UString( getIntf(), rData.m_tooltip.c_str() ),
528 UString( getIntf(), rData.m_help.c_str() ) );
530 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(), *pCursor,
531 *pCurve, *pVar, rData.m_thickness, pVisible,
532 UString( getIntf(), rData.m_help.c_str() ) );
534 // Compute the position of the control
535 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
536 rData.m_xPos, rData.m_yPos,
537 pCurve->getWidth(), pCurve->getHeight(),
540 pLayout->addControl( pBackground, pos, rData.m_layer );
541 pLayout->addControl( pCursor, pos, rData.m_layer );
543 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
544 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
548 void Builder::addList( const BuilderData::List &rData )
550 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
551 if( pLayout == NULL )
553 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
557 GenericFont *pFont = getFont( rData.m_fontId );
560 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
564 // Get the list variable
565 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
566 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
569 msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
573 // Get the visibility variable
574 // XXX check when it is null
575 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
577 // Create the list control
578 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont,
579 rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
580 rData.m_bgColor2, rData.m_selColor,
581 UString( getIntf(), rData.m_help.c_str() ), pVisible );
583 // Compute the position of the control
584 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
585 rData.m_xPos, rData.m_yPos,
586 rData.m_width, rData.m_height,
589 pLayout->addControl( pList, pos, rData.m_layer );
591 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
595 void Builder::addVideo( const BuilderData::Video &rData )
597 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
598 if( pLayout == NULL )
600 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
604 // Get the visibility variable
605 // XXX check when it is null
606 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
607 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
609 CtrlVideo *pVideo = new CtrlVideo( getIntf(),
610 UString( getIntf(), rData.m_help.c_str() ), pVisible );
612 // Compute the position of the control
613 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
614 rData.m_xPos, rData.m_yPos,
615 rData.m_width, rData.m_height,
618 pLayout->addControl( pVideo, pos, rData.m_layer );
620 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
624 const Position Builder::makePosition( const string &rLeftTop,
625 const string &rRightBottom,
626 int xPos, int yPos, int width,
627 int height, const Box &rBox ) const
629 int left = 0, top = 0, right = 0, bottom = 0;
630 Position::Ref_t refLeftTop = Position::kLeftTop;
631 Position::Ref_t refRightBottom = Position::kLeftTop;
633 int boxWidth = rBox.getWidth();
634 int boxHeight = rBox.getHeight();
636 // Position of the left top corner
637 if( rLeftTop == "lefttop" )
641 refLeftTop = Position::kLeftTop;
643 else if( rLeftTop == "righttop" )
645 left = xPos - boxWidth + 1;
647 refLeftTop = Position::kRightTop;
649 else if( rLeftTop == "leftbottom" )
652 top = yPos - boxHeight + 1;
653 refLeftTop = Position::kLeftBottom;
655 else if( rLeftTop == "rightbottom" )
657 left = xPos - boxWidth + 1;
658 top = yPos - boxHeight + 1;
659 refLeftTop = Position::kRightBottom;
662 // Position of the right bottom corner
663 if( rRightBottom == "lefttop" )
665 right = xPos + width - 1;
666 bottom = yPos + height - 1;
667 refRightBottom = Position::kLeftTop;
669 else if( rRightBottom == "righttop" )
671 right = xPos + width - boxWidth;
672 bottom = yPos + height - 1;
673 refRightBottom = Position::kRightTop;
675 else if( rRightBottom == "leftbottom" )
677 right = xPos + width - 1;
678 bottom = yPos + height - boxHeight;
679 refRightBottom = Position::kLeftBottom;
681 else if( rRightBottom == "rightbottom" )
683 right = xPos + width - boxWidth;
684 bottom = yPos + height - boxHeight;
685 refRightBottom = Position::kRightBottom;
688 return Position( left, top, right, bottom, rBox, refLeftTop,
693 GenericFont *Builder::getFont( const string &fontId )
695 GenericFont *pFont = m_pTheme->m_fonts[fontId].get();
696 if( !pFont && fontId == "defaultfont" )
699 string defaultFont = (string)getIntf()->p_libvlc->psz_vlcpath +
700 "\\skins2\\fonts\\FreeSans.ttf";
702 string defaultFont = (string)DATA_PATH + "/skins2/fonts/FreeSans.ttf";
704 pFont = new FT2Font( getIntf(), defaultFont, 12 );
707 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
719 Bezier *Builder::getPoints( const char *pTag ) const
721 vector<float> xBez, yBez;
725 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
733 "Slider points cannot have negative coordinates!" );
744 if( *(pTag++) != ',' )
750 // Create the Bezier curve
751 return new Bezier( getIntf(), xBez, yBez );