1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: builder.cpp,v 1.8 2004/03/01 18:33:31 asmax Exp $
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/generic_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 "../utils/position.hpp"
46 #include "../utils/var_bool.hpp"
47 #include "../utils/var_text.hpp"
50 Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData):
51 SkinObject( pIntf ), m_rData( rData ), m_pTheme( NULL )
56 CmdGeneric *Builder::parseAction( const string &rAction )
58 return Interpreter::instance( getIntf() )->parseAction( rAction, m_pTheme );
63 #define ADD_OBJECTS( type ) \
64 list<BuilderData::type>::const_iterator it##type; \
65 for( it##type = m_rData.m_list##type.begin(); \
66 it##type != m_rData.m_list##type.end(); it##type++ ) \
68 add##type( *it##type ); \
72 Theme *Builder::build()
74 m_pTheme = new Theme( getIntf() );
75 if( m_pTheme == NULL )
80 // Create everything from the data in the XML
82 ADD_OBJECTS( Bitmap );
84 ADD_OBJECTS( Window );
85 ADD_OBJECTS( Layout );
86 ADD_OBJECTS( Anchor );
87 ADD_OBJECTS( Button );
88 ADD_OBJECTS( Checkbox );
91 ADD_OBJECTS( RadialSlider );
92 ADD_OBJECTS( Slider );
99 // Macro to get a bitmap by its ID in the builder
100 #define GET_BMP( pBmp, id ) \
103 pBmp = m_pTheme->m_bitmaps[id].get(); \
106 msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
111 void Builder::addTheme( const BuilderData::Theme &rData )
113 m_pTheme->getWindowManager().setMagnetValue( rData.m_magnet );
114 m_pTheme->getWindowManager().setAlphaValue( rData.m_alpha );
115 m_pTheme->getWindowManager().setMoveAlphaValue( rData.m_moveAlpha );
119 void Builder::addBitmap( const BuilderData::Bitmap &rData )
121 GenericBitmap *pBmp = new PngBitmap( getIntf(), rData.m_fileName,
122 rData.m_alphaColor );
123 m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
127 void Builder::addFont( const BuilderData::Font &rData )
129 GenericFont *pFont = new FT2Font( getIntf(), rData.m_fontName,
132 m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
136 void Builder::addWindow( const BuilderData::Window &rData )
139 GenericFont *pFont = new FT2Font( getIntf(), "FreeSans.ttf", 12 );
141 GenericWindow *pWin =
142 new GenericWindow( getIntf(), rData.m_xPos, rData.m_yPos,
143 m_pTheme->getWindowManager(), *pFont,
144 rData.m_dragDrop, rData.m_playOnDrop );
146 m_pTheme->m_windows[rData.m_id] = GenericWindowPtr( pWin );
150 void Builder::addLayout( const BuilderData::Layout &rData )
152 GenericWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
155 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
159 int minWidth = rData.m_minWidth != -1 ? rData.m_minWidth : rData.m_width;
160 int maxWidth = rData.m_maxWidth != -1 ? rData.m_maxWidth : rData.m_width;
161 int minHeight = rData.m_minHeight != -1 ? rData.m_minHeight :
163 int maxHeight = rData.m_maxHeight != -1 ? rData.m_maxHeight :
165 GenericLayout *pLayout = new GenericLayout( getIntf(), rData.m_width,
167 minWidth, maxWidth, minHeight,
169 m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
171 // Attach the layout to its window
172 pWin->setActiveLayout( pLayout );
176 void Builder::addAnchor( const BuilderData::Anchor &rData )
178 GenericWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
181 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
185 Anchor *pAnc = new Anchor( getIntf(), rData.m_xPos, rData.m_yPos,
186 rData.m_range, rData.m_priority, *pWin );
187 pWin->addAnchor( pAnc );
191 void Builder::addButton( const BuilderData::Button &rData )
193 // Get the bitmaps of the button
194 GenericBitmap *pBmpUp = NULL;
195 GET_BMP( pBmpUp, rData.m_upId );
197 GenericBitmap *pBmpDown = pBmpUp;
198 GET_BMP( pBmpDown, rData.m_downId );
200 GenericBitmap *pBmpOver = pBmpUp;
201 GET_BMP( pBmpOver, rData.m_overId );
203 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
204 if( pLayout == NULL )
206 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
210 CmdGeneric *pCommand = parseAction( rData.m_actionId );
211 if( pCommand == NULL )
213 msg_Err( getIntf(), "Invalid action: %s", rData.m_actionId.c_str() );
217 CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
218 *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
219 UString( getIntf(), rData.m_help.c_str() ), NULL );
221 // Compute the position of the control
222 // XXX (we suppose all the images have the same size...)
223 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
224 rData.m_xPos, rData.m_yPos,
226 pBmpUp->getHeight(), *pLayout );
228 pLayout->addControl( pButton, pos, rData.m_layer );
230 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pButton );
234 void Builder::addCheckbox( const BuilderData::Checkbox &rData )
236 // Get the bitmaps of the checkbox
237 GenericBitmap *pBmpUp1 = NULL;
238 GET_BMP( pBmpUp1, rData.m_up1Id );
240 GenericBitmap *pBmpDown1 = pBmpUp1;
241 GET_BMP( pBmpDown1, rData.m_down1Id );
243 GenericBitmap *pBmpOver1 = pBmpUp1;
244 GET_BMP( pBmpOver1, rData.m_over1Id );
246 GenericBitmap *pBmpUp2 = NULL;
247 GET_BMP( pBmpUp2, rData.m_up2Id );
249 GenericBitmap *pBmpDown2 = pBmpUp2;
250 GET_BMP( pBmpDown2, rData.m_down2Id );
252 GenericBitmap *pBmpOver2 = pBmpUp2;
253 GET_BMP( pBmpOver2, rData.m_over2Id );
255 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
256 if( pLayout == NULL )
258 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
262 CmdGeneric *pCommand1 = parseAction( rData.m_action1 );
263 if( pCommand1 == NULL )
265 msg_Err( getIntf(), "Invalid action: %s", rData.m_action1.c_str() );
269 CmdGeneric *pCommand2 = parseAction( rData.m_action2 );
270 if( pCommand2 == NULL )
272 msg_Err( getIntf(), "Invalid action: %s", rData.m_action2.c_str() );
276 // Get the state variable
277 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
278 VarBool *pVar = pInterpreter->getVarBool( rData.m_state, m_pTheme );
281 // TODO: default state
285 // Create the control
286 CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
287 *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
288 *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
289 UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
290 UString( getIntf(), rData.m_help.c_str() ), NULL );
292 // Compute the position of the control
293 // XXX (we suppose all the images have the same size...)
294 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
295 rData.m_xPos, rData.m_yPos,
297 pBmpUp1->getHeight(), *pLayout );
299 pLayout->addControl( pCheckbox, pos, rData.m_layer );
301 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCheckbox );
305 void Builder::addImage( const BuilderData::Image &rData )
307 GenericBitmap *pBmp = NULL;
308 GET_BMP( pBmp, rData.m_bmpId );
310 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
311 if( pLayout == NULL )
313 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
317 GenericWindow *pWindow = m_pTheme->m_windows[rData.m_windowId].get();
318 if( pWindow == NULL )
320 msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
324 CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp,
325 UString( getIntf(), rData.m_help.c_str() ),
328 // Compute the position of the control
329 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
331 rData.m_yPos, pBmp->getWidth(),
332 pBmp->getHeight(), *pLayout );
334 // XXX: test to be changed! XXX
335 if( rData.m_onclickId == "move" )
337 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
338 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ),
340 pLayout->addControl( pMove, pos, rData.m_layer );
342 else if( rData.m_onclickId == "resize" )
344 CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
345 UString( getIntf(), rData.m_help.c_str() ), NULL );
346 pLayout->addControl( pResize, pos, rData.m_layer );
350 pLayout->addControl( pImage, pos, rData.m_layer );
353 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pImage );
357 void Builder::addText( const BuilderData::Text &rData )
359 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
360 if( pLayout == NULL )
362 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
366 GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
369 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
373 // Create a text variable
374 VarText *pVar = new VarText( getIntf() );
375 UString msg( getIntf(), rData.m_text.c_str() );
377 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
379 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
380 UString( getIntf(), rData.m_help.c_str() ), rData.m_color, NULL );
382 int height = pFont->getSize();
384 pLayout->addControl( pText, Position( rData.m_xPos, rData.m_yPos,
385 rData.m_xPos + rData.m_width,
386 rData.m_yPos + height, *pLayout ),
389 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
393 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
395 // Get the bitmaps of the slider
396 GenericBitmap *pSeq = NULL;
397 GET_BMP( pSeq, rData.m_sequence );
399 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
400 if( pLayout == NULL )
402 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
406 // Get the variable associated to the slider
407 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
408 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
411 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
415 // Create the control
416 CtrlRadialSlider *pRadial =
417 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
418 rData.m_minAngle, rData.m_maxAngle,
419 UString( getIntf(), rData.m_help.c_str() ),
422 // XXX: resizing is not supported
423 // Compute the position of the control
425 makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
426 rData.m_yPos, pSeq->getWidth(),
427 pSeq->getHeight() / rData.m_nbImages, *pLayout );
429 pLayout->addControl( pRadial, pos, rData.m_layer );
431 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pRadial );
435 void Builder::addSlider( const BuilderData::Slider &rData )
437 // Get the bitmaps of the slider
438 GenericBitmap *pBmpUp = NULL;
439 GET_BMP( pBmpUp, rData.m_upId );
441 GenericBitmap *pBmpDown = pBmpUp;
442 GET_BMP( pBmpDown, rData.m_downId );
444 GenericBitmap *pBmpOver = pBmpUp;
445 GET_BMP( pBmpOver, rData.m_overId );
447 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
448 if( pLayout == NULL )
450 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
454 Bezier *pCurve = getPoints( rData.m_points.c_str() );
457 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
458 rData.m_points.c_str() );
461 m_pTheme->m_curves.push_back( BezierPtr( pCurve ) );
463 // Get the visibility variable
464 // XXX check when it is null
465 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
466 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
468 // Get the variable associated to the slider
469 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
472 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
476 // Create the cursor and background controls
477 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
478 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
479 UString( getIntf(), rData.m_tooltip.c_str() ),
480 UString( getIntf(), rData.m_help.c_str() ) );
482 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(), *pCursor,
483 *pCurve, *pVar, rData.m_thickness, pVisible,
484 UString( getIntf(), rData.m_help.c_str() ) );
486 // Compute the position of the control
487 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
488 rData.m_xPos, rData.m_yPos,
489 pCurve->getWidth(), pCurve->getHeight(),
492 pLayout->addControl( pBackground, pos, rData.m_layer );
493 pLayout->addControl( pCursor, pos, rData.m_layer );
495 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pCursor );
496 m_pTheme->m_controls[rData.m_id + "_bg"] = CtrlGenericPtr( pBackground );
500 void Builder::addList( const BuilderData::List &rData )
502 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
503 if( pLayout == NULL )
505 msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
509 GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
512 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
516 // Get the list variable
517 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
518 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
521 msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
525 // Create the list control
526 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont,
527 rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
528 rData.m_bgColor2, rData.m_selColor,
529 UString( getIntf(), rData.m_help.c_str() ), NULL );
531 // Compute the position of the control
532 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
533 rData.m_xPos, rData.m_yPos,
534 rData.m_width, rData.m_height,
537 pLayout->addControl( pList, pos, rData.m_layer );
539 m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pList );
543 const Position Builder::makePosition( const string &rLeftTop,
544 const string &rRightBottom,
545 int xPos, int yPos, int width,
546 int height, const Box &rBox ) const
548 int left = 0, top = 0, right = 0, bottom = 0;
549 Position::Ref_t refLeftTop = Position::kLeftTop;
550 Position::Ref_t refRightBottom = Position::kLeftTop;
552 int boxWidth = rBox.getWidth();
553 int boxHeight = rBox.getHeight();
555 // Position of the left top corner
556 if( rLeftTop == "lefttop" )
560 refLeftTop = Position::kLeftTop;
562 else if( rLeftTop == "righttop" )
564 left = xPos - boxWidth + 1;
566 refLeftTop = Position::kRightTop;
568 else if( rLeftTop == "leftbottom" )
571 top = yPos - boxHeight + 1;
572 refLeftTop = Position::kLeftBottom;
574 else if( rLeftTop == "rightbottom" )
576 left = xPos - boxWidth + 1;
577 top = yPos - boxHeight + 1;
578 refLeftTop = Position::kRightBottom;
581 // Position of the right bottom corner
582 if( rRightBottom == "lefttop" )
584 right = xPos + width - 1;
585 bottom = yPos + height - 1;
586 refRightBottom = Position::kLeftTop;
588 else if( rRightBottom == "righttop" )
590 right = xPos + width - boxWidth;
591 bottom = yPos + height - 1;
592 refRightBottom = Position::kRightTop;
594 else if( rRightBottom == "leftbottom" )
596 right = xPos + width - 1;
597 bottom = yPos + height - boxHeight;
598 refRightBottom = Position::kLeftBottom;
600 else if( rRightBottom == "rightbottom" )
602 right = xPos + width - boxWidth;
603 bottom = yPos + height - boxHeight;
604 refRightBottom = Position::kRightBottom;
607 return Position( left, top, right, bottom, rBox, refLeftTop,
612 Bezier *Builder::getPoints( const char *pTag ) const
614 vector<float> xBez, yBez;
618 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 2 )
625 "Slider points cannot have negative coordinates!" );
635 if( *(pTag++) != ',' )
641 // Create the Bezier curve
642 return new Bezier( getIntf(), xBez, yBez );