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 = new FT2Font( getIntf(), "FreeSans.ttf", 12 );
124 void Builder::addBitmap( const BuilderData::Bitmap &rData )
126 GenericBitmap *pBmp = new PngBitmap( getIntf(), rData.m_fileName,
127 rData.m_alphaColor );
128 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
132 void Builder::addFont( const BuilderData::Font &rData )
134 GenericFont *pFont = new FT2Font( getIntf(), rData.m_fontName,
138 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
147 void Builder::addWindow( const BuilderData::Window &rData )
150 new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
151 m_pTheme->getWindowManager(),
152 rData.m_dragDrop, rData.m_playOnDrop );
154 m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
158 void Builder::addLayout( const BuilderData::Layout &rData )
160 TopWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
163 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
167 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
168 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
169 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
171 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
173 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
175 minWidth, maxWidth, minHeight,
177 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
179 // Attach the layout to its window
180 pWin->setActiveLayout( pLayout );
184 void Builder::addAnchor( const BuilderData::Anchor &rData )
186 TopWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
189 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
193 Bezier *pCurve = getPoints( rData.m_points.c_str() );
196 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
197 rData.m_points.c_str() );
200 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
202 Anchor *pAnc = new Anchor( getIntf(), rData.m_xPos, rData.m_yPos,
203 rData.m_range, rData.m_priority,
205 pWin->addAnchor( pAnc );
209 void Builder::addButton( const BuilderData::Button &rData )
211 // Get the bitmaps of the button
212 GenericBitmap *pBmpUp = NULL;
213 GET_BMP( pBmpUp, rData.m_upId );
215 GenericBitmap *pBmpDown = pBmpUp;
216 GET_BMP( pBmpDown, rData.m_downId );
218 GenericBitmap *pBmpOver = pBmpUp;
219 GET_BMP( pBmpOver, rData.m_overId );
221 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
222 if( pLayout == NULL )
224 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
228 CmdGeneric *pCommand = parseAction( rData.m_actionId );
229 if( pCommand == NULL )
231 msg_Err( getIntf(), "Invalid action: %s", rData.m_actionId.c_str() );
235 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
236 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
237 UString( getIntf(), rData.m_help.c_str() ), NULL );
239 // Compute the position of the control
240 // XXX (we suppose all the images have the same size...)
241 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
242 rData.m_xPos, rData.m_yPos,
244 pBmpUp->getHeight(), *pLayout );
246 pLayout->addControl( pButton, pos, rData.m_layer );
248 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
252 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
254 // Get the bitmaps of the checkbox
255 GenericBitmap *pBmpUp1 = NULL;
256 GET_BMP( pBmpUp1, rData.m_up1Id );
258 GenericBitmap *pBmpDown1 = pBmpUp1;
259 GET_BMP( pBmpDown1, rData.m_down1Id );
261 GenericBitmap *pBmpOver1 = pBmpUp1;
262 GET_BMP( pBmpOver1, rData.m_over1Id );
264 GenericBitmap *pBmpUp2 = NULL;
265 GET_BMP( pBmpUp2, rData.m_up2Id );
267 GenericBitmap *pBmpDown2 = pBmpUp2;
268 GET_BMP( pBmpDown2, rData.m_down2Id );
270 GenericBitmap *pBmpOver2 = pBmpUp2;
271 GET_BMP( pBmpOver2, rData.m_over2Id );
273 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
274 if( pLayout == NULL )
276 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
280 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
281 if( pCommand1 == NULL )
283 msg_Err( getIntf(), "Invalid action: %s", rData.m_action1.c_str() );
287 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
288 if( pCommand2 == NULL )
290 msg_Err( getIntf(), "Invalid action: %s", rData.m_action2.c_str() );
294 // Get the state variable
295 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
296 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
299 // TODO: default state
303 // Create the control
304 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
305 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
306 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
307 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
308 UString( getIntf(), rData.m_help.c_str() ), NULL );
310 // Compute the position of the control
311 // XXX (we suppose all the images have the same size...)
312 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
313 rData.m_xPos, rData.m_yPos,
315 pBmpUp1->getHeight(), *pLayout );
317 pLayout->addControl( pCheckbox, pos, rData.m_layer );
319 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
323 void Builder::addImage( const BuilderData::Image &rData )
325 GenericBitmap *pBmp = NULL;
326 GET_BMP( pBmp, rData.m_bmpId );
328 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
329 if( pLayout == NULL )
331 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
335 TopWindow *pWindow = m_pTheme->m_windows[rData.m_windowId].get();
336 if( pWindow == NULL )
338 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
342 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp,
343 UString( getIntf(), rData.m_help.c_str() ),
346 // Compute the position of the control
347 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
349 rData.m_yPos, pBmp->getWidth(),
350 pBmp->getHeight(), *pLayout );
352 // XXX: test to be changed! XXX
353 if( rData.m_actionId == "move" )
355 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
356 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
358 pLayout->addControl( pMove, pos, rData.m_layer );
360 else if( rData.m_actionId == "resize" )
362 CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
363 UString( getIntf(), rData.m_help.c_str() ), NULL );
364 pLayout->addControl( pResize, pos, rData.m_layer );
368 pLayout->addControl( pImage, pos, rData.m_layer );
371 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
375 void Builder::addText( const BuilderData::Text &rData )
377 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
378 if( pLayout == NULL )
380 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
384 GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
387 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
391 // Create a text variable
392 VarText *pVar = new VarText( getIntf() );
393 UString msg( getIntf(), rData.m_text.c_str() );
395 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
397 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
398 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, NULL );
400 int height = pFont->getSize();
402 pLayout->addControl( pText, Position( rData.m_xPos, rData.m_yPos,
403 rData.m_xPos + rData.m_width,
404 rData.m_yPos + height, *pLayout ),
407 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
411 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
413 // Get the bitmaps of the slider
414 GenericBitmap *pSeq = NULL;
415 GET_BMP( pSeq, rData.m_sequence );
417 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
418 if( pLayout == NULL )
420 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
424 // Get the variable associated to the slider
425 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
426 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
429 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
433 // Create the control
434 CtrlRadialSlider *pRadial =
435 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
436 rData.m_minAngle, rData.m_maxAngle,
437 UString( getIntf(), rData.m_help.c_str() ),
440 // XXX: resizing is not supported
441 // Compute the position of the control
443 makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
444 rData.m_yPos, pSeq->getWidth(),
445 pSeq->getHeight() / rData.m_nbImages, *pLayout );
447 pLayout->addControl( pRadial, pos, rData.m_layer );
449 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
453 void Builder::addSlider( const BuilderData::Slider &rData )
455 // Get the bitmaps of the slider
456 GenericBitmap *pBmpUp = NULL;
457 GET_BMP( pBmpUp, rData.m_upId );
459 GenericBitmap *pBmpDown = pBmpUp;
460 GET_BMP( pBmpDown, rData.m_downId );
462 GenericBitmap *pBmpOver = pBmpUp;
463 GET_BMP( pBmpOver, rData.m_overId );
465 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
466 if( pLayout == NULL )
468 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
472 Bezier *pCurve = getPoints( rData.m_points.c_str() );
475 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
476 rData.m_points.c_str() );
479 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
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 // Get the variable associated to the slider
487 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
490 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
494 // Create the cursor and background controls
495 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
496 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
497 UString( getIntf(), rData.m_tooltip.c_str() ),
498 UString( getIntf(), rData.m_help.c_str() ) );
500 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(), *pCursor,
501 *pCurve, *pVar, rData.m_thickness, pVisible,
502 UString( getIntf(), rData.m_help.c_str() ) );
504 // Compute the position of the control
505 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
506 rData.m_xPos, rData.m_yPos,
507 pCurve->getWidth(), pCurve->getHeight(),
510 pLayout->addControl( pBackground, pos, rData.m_layer );
511 pLayout->addControl( pCursor, pos, rData.m_layer );
513 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
514 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
518 void Builder::addList( const BuilderData::List &rData )
520 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
521 if( pLayout == NULL )
523 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
527 GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
530 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
534 // Get the list variable
535 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
536 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
539 msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
543 // Create the list control
544 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont,
545 rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
546 rData.m_bgColor2, rData.m_selColor,
547 UString( getIntf(), rData.m_help.c_str() ), NULL );
549 // Compute the position of the control
550 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
551 rData.m_xPos, rData.m_yPos,
552 rData.m_width, rData.m_height,
555 pLayout->addControl( pList, pos, rData.m_layer );
557 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
561 void Builder::addVideo( const BuilderData::Video &rData )
563 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
564 if( pLayout == NULL )
566 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
570 CtrlVideo *pVideo = new CtrlVideo( getIntf(),
571 UString( getIntf(), rData.m_help.c_str() ), NULL);
573 // Compute the position of the control
574 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
575 rData.m_xPos, rData.m_yPos,
576 rData.m_width, rData.m_height,
579 pLayout->addControl( pVideo, pos, rData.m_layer );
581 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
585 const Position Builder::makePosition( const string &rLeftTop,
586 const string &rRightBottom,
587 int xPos, int yPos, int width,
588 int height, const Box &rBox ) const
590 int left = 0, top = 0, right = 0, bottom = 0;
591 Position::Ref_t refLeftTop = Position::kLeftTop;
592 Position::Ref_t refRightBottom = Position::kLeftTop;
594 int boxWidth = rBox.getWidth();
595 int boxHeight = rBox.getHeight();
597 // Position of the left top corner
598 if( rLeftTop == "lefttop" )
602 refLeftTop = Position::kLeftTop;
604 else if( rLeftTop == "righttop" )
606 left = xPos - boxWidth + 1;
608 refLeftTop = Position::kRightTop;
610 else if( rLeftTop == "leftbottom" )
613 top = yPos - boxHeight + 1;
614 refLeftTop = Position::kLeftBottom;
616 else if( rLeftTop == "rightbottom" )
618 left = xPos - boxWidth + 1;
619 top = yPos - boxHeight + 1;
620 refLeftTop = Position::kRightBottom;
623 // Position of the right bottom corner
624 if( rRightBottom == "lefttop" )
626 right = xPos + width - 1;
627 bottom = yPos + height - 1;
628 refRightBottom = Position::kLeftTop;
630 else if( rRightBottom == "righttop" )
632 right = xPos + width - boxWidth;
633 bottom = yPos + height - 1;
634 refRightBottom = Position::kRightTop;
636 else if( rRightBottom == "leftbottom" )
638 right = xPos + width - 1;
639 bottom = yPos + height - boxHeight;
640 refRightBottom = Position::kLeftBottom;
642 else if( rRightBottom == "rightbottom" )
644 right = xPos + width - boxWidth;
645 bottom = yPos + height - boxHeight;
646 refRightBottom = Position::kRightBottom;
649 return Position( left, top, right, bottom, rBox, refLeftTop,
654 Bezier *Builder::getPoints( const char *pTag ) const
656 vector<float> xBez, yBez;
660 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
668 "Slider points cannot have negative coordinates!" );
679 if( *(pTag++) != ',' )
685 // Create the Bezier curve
686 return new Bezier( getIntf(), xBez, yBez );