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_fontName,
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 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
244 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
245 UString( getIntf(), rData.m_help.c_str() ), NULL );
247 // Compute the position of the control
248 // XXX (we suppose all the images have the same size...)
249 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
250 rData.m_xPos, rData.m_yPos,
252 pBmpUp->getHeight(), *pLayout );
254 pLayout->addControl( pButton, pos, rData.m_layer );
256 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
260 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
262 // Get the bitmaps of the checkbox
263 GenericBitmap *pBmpUp1 = NULL;
264 GET_BMP( pBmpUp1, rData.m_up1Id );
266 GenericBitmap *pBmpDown1 = pBmpUp1;
267 GET_BMP( pBmpDown1, rData.m_down1Id );
269 GenericBitmap *pBmpOver1 = pBmpUp1;
270 GET_BMP( pBmpOver1, rData.m_over1Id );
272 GenericBitmap *pBmpUp2 = NULL;
273 GET_BMP( pBmpUp2, rData.m_up2Id );
275 GenericBitmap *pBmpDown2 = pBmpUp2;
276 GET_BMP( pBmpDown2, rData.m_down2Id );
278 GenericBitmap *pBmpOver2 = pBmpUp2;
279 GET_BMP( pBmpOver2, rData.m_over2Id );
281 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
282 if( pLayout == NULL )
284 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
288 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
289 if( pCommand1 == NULL )
291 msg_Err( getIntf(), "Invalid action: %s", rData.m_action1.c_str() );
295 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
296 if( pCommand2 == NULL )
298 msg_Err( getIntf(), "Invalid action: %s", rData.m_action2.c_str() );
302 // Get the state variable
303 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
304 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
307 // TODO: default state
311 // Create the control
312 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
313 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
314 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
315 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
316 UString( getIntf(), rData.m_help.c_str() ), NULL );
318 // Compute the position of the control
319 // XXX (we suppose all the images have the same size...)
320 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
321 rData.m_xPos, rData.m_yPos,
323 pBmpUp1->getHeight(), *pLayout );
325 pLayout->addControl( pCheckbox, pos, rData.m_layer );
327 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
331 void Builder::addImage( const BuilderData::Image &rData )
333 GenericBitmap *pBmp = NULL;
334 GET_BMP( pBmp, rData.m_bmpId );
336 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
337 if( pLayout == NULL )
339 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
343 TopWindow *pWindow = m_pTheme->m_windows[rData.m_windowId].get();
344 if( pWindow == NULL )
346 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
350 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp,
351 UString( getIntf(), rData.m_help.c_str() ),
354 // Compute the position of the control
355 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
357 rData.m_yPos, pBmp->getWidth(),
358 pBmp->getHeight(), *pLayout );
360 // XXX: test to be changed! XXX
361 if( rData.m_actionId == "move" )
363 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
364 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
366 pLayout->addControl( pMove, pos, rData.m_layer );
368 else if( rData.m_actionId == "resize" )
370 CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
371 UString( getIntf(), rData.m_help.c_str() ), NULL );
372 pLayout->addControl( pResize, pos, rData.m_layer );
376 pLayout->addControl( pImage, pos, rData.m_layer );
379 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
383 void Builder::addText( const BuilderData::Text &rData )
385 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
386 if( pLayout == NULL )
388 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
392 GenericFont *pFont = getFont( rData.m_fontId );
395 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
399 // Create a text variable
400 VarText *pVar = new VarText( getIntf() );
401 UString msg( getIntf(), rData.m_text.c_str() );
403 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
405 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
406 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, NULL );
408 int height = pFont->getSize();
410 pLayout->addControl( pText, Position( rData.m_xPos, rData.m_yPos,
411 rData.m_xPos + rData.m_width,
412 rData.m_yPos + height, *pLayout ),
415 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
419 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
421 // Get the bitmaps of the slider
422 GenericBitmap *pSeq = NULL;
423 GET_BMP( pSeq, rData.m_sequence );
425 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
426 if( pLayout == NULL )
428 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
432 // Get the variable associated to the slider
433 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
434 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
437 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
441 // Create the control
442 CtrlRadialSlider *pRadial =
443 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
444 rData.m_minAngle, rData.m_maxAngle,
445 UString( getIntf(), rData.m_help.c_str() ),
448 // XXX: resizing is not supported
449 // Compute the position of the control
451 makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
452 rData.m_yPos, pSeq->getWidth(),
453 pSeq->getHeight() / rData.m_nbImages, *pLayout );
455 pLayout->addControl( pRadial, pos, rData.m_layer );
457 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
461 void Builder::addSlider( const BuilderData::Slider &rData )
463 // Get the bitmaps of the slider
464 GenericBitmap *pBmpUp = NULL;
465 GET_BMP( pBmpUp, rData.m_upId );
467 GenericBitmap *pBmpDown = pBmpUp;
468 GET_BMP( pBmpDown, rData.m_downId );
470 GenericBitmap *pBmpOver = pBmpUp;
471 GET_BMP( pBmpOver, rData.m_overId );
473 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
474 if( pLayout == NULL )
476 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
480 Bezier *pCurve = getPoints( rData.m_points.c_str() );
483 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
484 rData.m_points.c_str() );
487 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
489 // Get the visibility variable
490 // XXX check when it is null
491 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
492 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
494 // Get the variable associated to the slider
495 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
498 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
502 // Create the cursor and background controls
503 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
504 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
505 UString( getIntf(), rData.m_tooltip.c_str() ),
506 UString( getIntf(), rData.m_help.c_str() ) );
508 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(), *pCursor,
509 *pCurve, *pVar, rData.m_thickness, pVisible,
510 UString( getIntf(), rData.m_help.c_str() ) );
512 // Compute the position of the control
513 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
514 rData.m_xPos, rData.m_yPos,
515 pCurve->getWidth(), pCurve->getHeight(),
518 pLayout->addControl( pBackground, pos, rData.m_layer );
519 pLayout->addControl( pCursor, pos, rData.m_layer );
521 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
522 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
526 void Builder::addList( const BuilderData::List &rData )
528 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
529 if( pLayout == NULL )
531 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
535 GenericFont *pFont = getFont( rData.m_fontId );
538 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
542 // Get the list variable
543 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
544 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
547 msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
551 // Create the list control
552 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont,
553 rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
554 rData.m_bgColor2, rData.m_selColor,
555 UString( getIntf(), rData.m_help.c_str() ), NULL );
557 // Compute the position of the control
558 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
559 rData.m_xPos, rData.m_yPos,
560 rData.m_width, rData.m_height,
563 pLayout->addControl( pList, pos, rData.m_layer );
565 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
569 void Builder::addVideo( const BuilderData::Video &rData )
571 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
572 if( pLayout == NULL )
574 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
578 CtrlVideo *pVideo = new CtrlVideo( getIntf(),
579 UString( getIntf(), rData.m_help.c_str() ), NULL);
581 // Compute the position of the control
582 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
583 rData.m_xPos, rData.m_yPos,
584 rData.m_width, rData.m_height,
587 pLayout->addControl( pVideo, pos, rData.m_layer );
589 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
593 const Position Builder::makePosition( const string &rLeftTop,
594 const string &rRightBottom,
595 int xPos, int yPos, int width,
596 int height, const Box &rBox ) const
598 int left = 0, top = 0, right = 0, bottom = 0;
599 Position::Ref_t refLeftTop = Position::kLeftTop;
600 Position::Ref_t refRightBottom = Position::kLeftTop;
602 int boxWidth = rBox.getWidth();
603 int boxHeight = rBox.getHeight();
605 // Position of the left top corner
606 if( rLeftTop == "lefttop" )
610 refLeftTop = Position::kLeftTop;
612 else if( rLeftTop == "righttop" )
614 left = xPos - boxWidth + 1;
616 refLeftTop = Position::kRightTop;
618 else if( rLeftTop == "leftbottom" )
621 top = yPos - boxHeight + 1;
622 refLeftTop = Position::kLeftBottom;
624 else if( rLeftTop == "rightbottom" )
626 left = xPos - boxWidth + 1;
627 top = yPos - boxHeight + 1;
628 refLeftTop = Position::kRightBottom;
631 // Position of the right bottom corner
632 if( rRightBottom == "lefttop" )
634 right = xPos + width - 1;
635 bottom = yPos + height - 1;
636 refRightBottom = Position::kLeftTop;
638 else if( rRightBottom == "righttop" )
640 right = xPos + width - boxWidth;
641 bottom = yPos + height - 1;
642 refRightBottom = Position::kRightTop;
644 else if( rRightBottom == "leftbottom" )
646 right = xPos + width - 1;
647 bottom = yPos + height - boxHeight;
648 refRightBottom = Position::kLeftBottom;
650 else if( rRightBottom == "rightbottom" )
652 right = xPos + width - boxWidth;
653 bottom = yPos + height - boxHeight;
654 refRightBottom = Position::kRightBottom;
657 return Position( left, top, right, bottom, rBox, refLeftTop,
662 GenericFont *Builder::getFont( const string &fontId )
664 GenericFont *pFont = m_pTheme->m_fonts[fontId].get();
665 if( !pFont && fontId == "defaultfont" )
668 string defaultFont = (string)getIntf()->p_libvlc->psz_vlcpath +
669 "\\skins2\\fonts\\FreeSans.ttf";
671 string defaultFont = (string)DATA_PATH + "/skins2/fonts/FreeSans.ttf";
673 pFont = new FT2Font( getIntf(), defaultFont, 12 );
676 m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
688 Bezier *Builder::getPoints( const char *pTag ) const
690 vector<float> xBez, yBez;
694 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
702 "Slider points cannot have negative coordinates!" );
713 if( *(pTag++) != ',' )
719 // Create the Bezier curve
720 return new Bezier( getIntf(), xBez, yBez );