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 );
120 GenericFont *pFont = new FT2Font( getIntf(), "FreeSans.ttf", 12 );
122 rManager.createTooltip( *pFont );
126 void Builder::addBitmap( const BuilderData::Bitmap &rData )
128 GenericBitmap *pBmp = new PngBitmap( getIntf(), rData.m_fileName,
129 rData.m_alphaColor );
130 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
134 void Builder::addFont( const BuilderData::Font &rData )
136 GenericFont *pFont = new FT2Font( getIntf(), rData.m_fontName,
140 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
149 void Builder::addWindow( const BuilderData::Window &rData )
152 new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
153 m_pTheme->getWindowManager(),
154 rData.m_dragDrop, rData.m_playOnDrop );
156 m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
160 void Builder::addLayout( const BuilderData::Layout &rData )
162 TopWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
165 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
169 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
170 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
171 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
173 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
175 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
177 minWidth, maxWidth, minHeight,
179 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
181 // Attach the layout to its window
182 pWin->setActiveLayout( pLayout );
186 void Builder::addAnchor( const BuilderData::Anchor &rData )
188 TopWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
191 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
195 Bezier *pCurve = getPoints( rData.m_points.c_str() );
198 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
199 rData.m_points.c_str() );
202 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
204 Anchor *pAnc = new Anchor( getIntf(), rData.m_xPos, rData.m_yPos,
205 rData.m_range, rData.m_priority,
207 pWin->addAnchor( pAnc );
211 void Builder::addButton( const BuilderData::Button &rData )
213 // Get the bitmaps of the button
214 GenericBitmap *pBmpUp = NULL;
215 GET_BMP( pBmpUp, rData.m_upId );
217 GenericBitmap *pBmpDown = pBmpUp;
218 GET_BMP( pBmpDown, rData.m_downId );
220 GenericBitmap *pBmpOver = pBmpUp;
221 GET_BMP( pBmpOver, rData.m_overId );
223 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
224 if( pLayout == NULL )
226 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
230 CmdGeneric *pCommand = parseAction( rData.m_actionId );
231 if( pCommand == NULL )
233 msg_Err( getIntf(), "Invalid action: %s", rData.m_actionId.c_str() );
237 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
238 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
239 UString( getIntf(), rData.m_help.c_str() ), NULL );
241 // Compute the position of the control
242 // XXX (we suppose all the images have the same size...)
243 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
244 rData.m_xPos, rData.m_yPos,
246 pBmpUp->getHeight(), *pLayout );
248 pLayout->addControl( pButton, pos, rData.m_layer );
250 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
254 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
256 // Get the bitmaps of the checkbox
257 GenericBitmap *pBmpUp1 = NULL;
258 GET_BMP( pBmpUp1, rData.m_up1Id );
260 GenericBitmap *pBmpDown1 = pBmpUp1;
261 GET_BMP( pBmpDown1, rData.m_down1Id );
263 GenericBitmap *pBmpOver1 = pBmpUp1;
264 GET_BMP( pBmpOver1, rData.m_over1Id );
266 GenericBitmap *pBmpUp2 = NULL;
267 GET_BMP( pBmpUp2, rData.m_up2Id );
269 GenericBitmap *pBmpDown2 = pBmpUp2;
270 GET_BMP( pBmpDown2, rData.m_down2Id );
272 GenericBitmap *pBmpOver2 = pBmpUp2;
273 GET_BMP( pBmpOver2, rData.m_over2Id );
275 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
276 if( pLayout == NULL )
278 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
282 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
283 if( pCommand1 == NULL )
285 msg_Err( getIntf(), "Invalid action: %s", rData.m_action1.c_str() );
289 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
290 if( pCommand2 == NULL )
292 msg_Err( getIntf(), "Invalid action: %s", rData.m_action2.c_str() );
296 // Get the state variable
297 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
298 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
301 // TODO: default state
305 // Create the control
306 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
307 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
308 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
309 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
310 UString( getIntf(), rData.m_help.c_str() ), NULL );
312 // Compute the position of the control
313 // XXX (we suppose all the images have the same size...)
314 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
315 rData.m_xPos, rData.m_yPos,
317 pBmpUp1->getHeight(), *pLayout );
319 pLayout->addControl( pCheckbox, pos, rData.m_layer );
321 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
325 void Builder::addImage( const BuilderData::Image &rData )
327 GenericBitmap *pBmp = NULL;
328 GET_BMP( pBmp, rData.m_bmpId );
330 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
331 if( pLayout == NULL )
333 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
337 TopWindow *pWindow = m_pTheme->m_windows[rData.m_windowId].get();
338 if( pWindow == NULL )
340 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
344 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp,
345 UString( getIntf(), rData.m_help.c_str() ),
348 // Compute the position of the control
349 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
351 rData.m_yPos, pBmp->getWidth(),
352 pBmp->getHeight(), *pLayout );
354 // XXX: test to be changed! XXX
355 if( rData.m_actionId == "move" )
357 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
358 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
360 pLayout->addControl( pMove, pos, rData.m_layer );
362 else if( rData.m_actionId == "resize" )
364 CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
365 UString( getIntf(), rData.m_help.c_str() ), NULL );
366 pLayout->addControl( pResize, pos, rData.m_layer );
370 pLayout->addControl( pImage, pos, rData.m_layer );
373 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
377 void Builder::addText( const BuilderData::Text &rData )
379 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
380 if( pLayout == NULL )
382 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
386 GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
389 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
393 // Create a text variable
394 VarText *pVar = new VarText( getIntf() );
395 UString msg( getIntf(), rData.m_text.c_str() );
397 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
399 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
400 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, NULL );
402 int height = pFont->getSize();
404 pLayout->addControl( pText, Position( rData.m_xPos, rData.m_yPos,
405 rData.m_xPos + rData.m_width,
406 rData.m_yPos + height, *pLayout ),
409 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
413 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
415 // Get the bitmaps of the slider
416 GenericBitmap *pSeq = NULL;
417 GET_BMP( pSeq, rData.m_sequence );
419 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
420 if( pLayout == NULL )
422 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
426 // Get the variable associated to the slider
427 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
428 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
431 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
435 // Create the control
436 CtrlRadialSlider *pRadial =
437 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
438 rData.m_minAngle, rData.m_maxAngle,
439 UString( getIntf(), rData.m_help.c_str() ),
442 // XXX: resizing is not supported
443 // Compute the position of the control
445 makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
446 rData.m_yPos, pSeq->getWidth(),
447 pSeq->getHeight() / rData.m_nbImages, *pLayout );
449 pLayout->addControl( pRadial, pos, rData.m_layer );
451 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
455 void Builder::addSlider( const BuilderData::Slider &rData )
457 // Get the bitmaps of the slider
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->m_layouts[rData.m_layoutId].get();
468 if( pLayout == NULL )
470 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
474 Bezier *pCurve = getPoints( rData.m_points.c_str() );
477 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
478 rData.m_points.c_str() );
481 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
483 // Get the visibility variable
484 // XXX check when it is null
485 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
486 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
488 // Get the variable associated to the slider
489 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
492 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
496 // Create the cursor and background controls
497 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
498 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
499 UString( getIntf(), rData.m_tooltip.c_str() ),
500 UString( getIntf(), rData.m_help.c_str() ) );
502 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(), *pCursor,
503 *pCurve, *pVar, rData.m_thickness, pVisible,
504 UString( getIntf(), rData.m_help.c_str() ) );
506 // Compute the position of the control
507 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
508 rData.m_xPos, rData.m_yPos,
509 pCurve->getWidth(), pCurve->getHeight(),
512 pLayout->addControl( pBackground, pos, rData.m_layer );
513 pLayout->addControl( pCursor, pos, rData.m_layer );
515 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
516 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
520 void Builder::addList( const BuilderData::List &rData )
522 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
523 if( pLayout == NULL )
525 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
529 GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
532 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
536 // Get the list variable
537 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
538 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
541 msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
545 // Create the list control
546 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont,
547 rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
548 rData.m_bgColor2, rData.m_selColor,
549 UString( getIntf(), rData.m_help.c_str() ), NULL );
551 // Compute the position of the control
552 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
553 rData.m_xPos, rData.m_yPos,
554 rData.m_width, rData.m_height,
557 pLayout->addControl( pList, pos, rData.m_layer );
559 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
563 void Builder::addVideo( const BuilderData::Video &rData )
565 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
566 if( pLayout == NULL )
568 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
572 CtrlVideo *pVideo = new CtrlVideo( getIntf(),
573 UString( getIntf(), rData.m_help.c_str() ), NULL);
575 // Compute the position of the control
576 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
577 rData.m_xPos, rData.m_yPos,
578 rData.m_width, rData.m_height,
581 pLayout->addControl( pVideo, pos, rData.m_layer );
583 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
587 const Position Builder::makePosition( const string &rLeftTop,
588 const string &rRightBottom,
589 int xPos, int yPos, int width,
590 int height, const Box &rBox ) const
592 int left = 0, top = 0, right = 0, bottom = 0;
593 Position::Ref_t refLeftTop = Position::kLeftTop;
594 Position::Ref_t refRightBottom = Position::kLeftTop;
596 int boxWidth = rBox.getWidth();
597 int boxHeight = rBox.getHeight();
599 // Position of the left top corner
600 if( rLeftTop == "lefttop" )
604 refLeftTop = Position::kLeftTop;
606 else if( rLeftTop == "righttop" )
608 left = xPos - boxWidth + 1;
610 refLeftTop = Position::kRightTop;
612 else if( rLeftTop == "leftbottom" )
615 top = yPos - boxHeight + 1;
616 refLeftTop = Position::kLeftBottom;
618 else if( rLeftTop == "rightbottom" )
620 left = xPos - boxWidth + 1;
621 top = yPos - boxHeight + 1;
622 refLeftTop = Position::kRightBottom;
625 // Position of the right bottom corner
626 if( rRightBottom == "lefttop" )
628 right = xPos + width - 1;
629 bottom = yPos + height - 1;
630 refRightBottom = Position::kLeftTop;
632 else if( rRightBottom == "righttop" )
634 right = xPos + width - boxWidth;
635 bottom = yPos + height - 1;
636 refRightBottom = Position::kRightTop;
638 else if( rRightBottom == "leftbottom" )
640 right = xPos + width - 1;
641 bottom = yPos + height - boxHeight;
642 refRightBottom = Position::kLeftBottom;
644 else if( rRightBottom == "rightbottom" )
646 right = xPos + width - boxWidth;
647 bottom = yPos + height - boxHeight;
648 refRightBottom = Position::kRightBottom;
651 return Position( left, top, right, bottom, rBox, refLeftTop,
656 Bezier *Builder::getPoints( const char *pTag ) const
658 vector<float> xBez, yBez;
662 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 1 )
670 "Slider points cannot have negative coordinates!" );
681 if( *(pTag++) != ',' )
687 // Create the Bezier curve
688 return new Bezier( getIntf(), xBez, yBez );