1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: builder.cpp,v 1.3 2004/01/25 11:44:19 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[uniqueId( 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[uniqueId( 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() ) );
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[uniqueId( 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() ) );
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[uniqueId( 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() ) );
327 // Compute the position of the control
328 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
330 rData.m_yPos, pBmp->getWidth(),
331 pBmp->getHeight(), *pLayout );
333 // XXX: test to be changed! XXX
334 if( rData.m_onclickId == "move" )
336 CtrlMove *pMove = new CtrlMove( getIntf(), m_pTheme->getWindowManager(),
337 *pImage, *pWindow, UString( getIntf(), rData.m_help.c_str() ) );
338 pLayout->addControl( pMove, pos, rData.m_layer );
340 else if( rData.m_onclickId == "resize" )
342 CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
343 UString( getIntf(), rData.m_help.c_str() ) );
344 pLayout->addControl( pResize, pos, rData.m_layer );
348 pLayout->addControl( pImage, pos, rData.m_layer );
351 m_pTheme->m_controls[uniqueId( rData.m_id) ] = CtrlGenericPtr( pImage );
355 void Builder::addText( const BuilderData::Text &rData )
357 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
358 if( pLayout == NULL )
360 msg_Err( getIntf(), "Unknown Layout id: %s", rData.m_layoutId.c_str() );
364 GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
367 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
371 // Create a text variable
372 VarText *pVar = new VarText( getIntf() );
373 UString msg( getIntf(), rData.m_text.c_str() );
375 m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
377 CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
378 UString( getIntf(), rData.m_help.c_str() ) );
380 int height = pFont->getSize();
382 pLayout->addControl( pText, Position( rData.m_xPos, rData.m_yPos,
383 rData.m_xPos + rData.m_width,
384 rData.m_yPos + height, *pLayout ),
387 m_pTheme->m_controls[uniqueId( rData.m_id) ] = CtrlGenericPtr( pText );
391 void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
393 // Get the bitmaps of the slider
394 GenericBitmap *pSeq = NULL;
395 GET_BMP( pSeq, rData.m_sequence );
397 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
398 if( pLayout == NULL )
400 msg_Err( getIntf(), "Unknown Layout id: %s", rData.m_layoutId.c_str() );
404 // Get the variable associated to the slider
405 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
406 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
409 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
413 // Create the control
414 CtrlRadialSlider *pRadial =
415 new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
416 rData.m_minAngle, rData.m_maxAngle,
417 UString( getIntf(), rData.m_help.c_str() ) );
419 // XXX: resizing is not supported
420 // Compute the position of the control
422 makePosition( rData.m_leftTop, rData.m_rightBottom, rData.m_xPos,
423 rData.m_yPos, pSeq->getWidth(),
424 pSeq->getHeight() / rData.m_nbImages, *pLayout );
426 pLayout->addControl( pRadial, pos, rData.m_layer );
428 m_pTheme->m_controls[uniqueId( rData.m_id) ] = CtrlGenericPtr( pRadial );
432 void Builder::addSlider( const BuilderData::Slider &rData )
434 // Get the bitmaps of the slider
435 GenericBitmap *pBmpUp = NULL;
436 GET_BMP( pBmpUp, rData.m_upId );
438 GenericBitmap *pBmpDown = pBmpUp;
439 GET_BMP( pBmpDown, rData.m_downId );
441 GenericBitmap *pBmpOver = pBmpUp;
442 GET_BMP( pBmpOver, rData.m_overId );
444 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
445 if( pLayout == NULL )
447 msg_Err( getIntf(), "Unknown Layout id: %s", rData.m_layoutId.c_str() );
452 Bezier *pCurve = getPoints( rData.m_points.c_str() );
455 msg_Err( getIntf(), "Invalid format in tag points=\"%s\"",
456 rData.m_points.c_str() );
460 // Get the visibility variable
461 // XXX check when it is null
462 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
463 VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
465 // Get the variable associated to the slider
466 VarPercent *pVar = pInterpreter->getVarPercent( rData.m_value, m_pTheme );
469 msg_Err( getIntf(), "Unknown slider value: %s", rData.m_value.c_str() );
473 // Create the cursor and background controls
474 CtrlSliderCursor *pCursor = new CtrlSliderCursor( getIntf(), *pBmpUp,
475 *pBmpOver, *pBmpDown, *pCurve, *pVar, pVisible,
476 UString( getIntf(), rData.m_tooltip.c_str() ),
477 UString( getIntf(), rData.m_help.c_str() ) );
479 CtrlSliderBg *pBackground = new CtrlSliderBg( getIntf(), *pCursor,
480 *pCurve, *pVar, rData.m_thickness, pVisible,
481 UString( getIntf(), rData.m_help.c_str() ) );
483 // Compute the position of the control
484 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
485 rData.m_xPos, rData.m_yPos,
486 pCurve->getWidth(), pCurve->getHeight(),
489 pLayout->addControl( pBackground, pos, rData.m_layer );
490 pLayout->addControl( pCursor, pos, rData.m_layer );
492 string newId = uniqueId( rData.m_id );
493 m_pTheme->m_controls[newId] = CtrlGenericPtr( pCursor );
494 m_pTheme->m_controls[newId + "_bg"] = CtrlGenericPtr( pBackground );
498 void Builder::addList( const BuilderData::List &rData )
500 GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
501 if( pLayout == NULL )
503 msg_Err( getIntf(), "Unknown Layout id: %s", rData.m_layoutId.c_str() );
507 GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
510 msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
514 // Get the list variable
515 Interpreter *pInterpreter = Interpreter::instance( getIntf() );
516 VarList *pVar = pInterpreter->getVarList( rData.m_var, m_pTheme );
519 msg_Err( getIntf(), "No such list variable: %s", rData.m_var.c_str() );
523 // Create the list control
524 CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont,
525 rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
526 rData.m_bgColor2, rData.m_selColor,
527 UString( getIntf(), rData.m_help.c_str() ) );
529 // Compute the position of the control
530 const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
531 rData.m_xPos, rData.m_yPos,
532 rData.m_width, rData.m_height,
535 pLayout->addControl( pList, pos, rData.m_layer );
537 m_pTheme->m_controls[uniqueId( rData.m_id) ] = CtrlGenericPtr( pList );
541 const string Builder::generateId() const
545 const string base = "_ReservedId_";
546 char genId[base.size() + 4];
547 snprintf( genId, base.size() + 4, "%s%i", base.c_str(), i );
553 const string Builder::uniqueId( const string &id )
557 if( m_idSet.find( id ) != m_idSet.end() )
559 // The id was already used
562 msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
564 newId = generateId();
568 // OK, this is a new id
572 // Add the id to the set
573 m_idSet.insert( newId );
580 const Position Builder::makePosition( const string &rLeftTop,
581 const string &rRightBottom,
582 int xPos, int yPos, int width,
583 int height, const Box &rBox ) const
585 int left = 0, top = 0, right = 0, bottom = 0;
586 Position::Ref_t refLeftTop = Position::kLeftTop;
587 Position::Ref_t refRightBottom = Position::kLeftTop;
589 int boxWidth = rBox.getWidth();
590 int boxHeight = rBox.getHeight();
592 // Position of the left top corner
593 if( rLeftTop == "lefttop" )
597 refLeftTop = Position::kLeftTop;
599 else if( rLeftTop == "righttop" )
601 left = xPos - boxWidth + 1;
603 refLeftTop = Position::kRightTop;
605 else if( rLeftTop == "leftbottom" )
608 top = yPos - boxHeight + 1;
609 refLeftTop = Position::kLeftBottom;
611 else if( rLeftTop == "rightbottom" )
613 left = xPos - boxWidth + 1;
614 top = yPos - boxHeight + 1;
615 refLeftTop = Position::kRightBottom;
618 // Position of the right bottom corner
619 if( rRightBottom == "lefttop" )
621 right = xPos + width - 1;
622 bottom = yPos + height - 1;
623 refRightBottom = Position::kLeftTop;
625 else if( rRightBottom == "righttop" )
627 right = xPos + width - boxWidth;
628 bottom = yPos + height - 1;
629 refRightBottom = Position::kRightTop;
631 else if( rRightBottom == "leftbottom" )
633 right = xPos + width - 1;
634 bottom = yPos + height - boxHeight;
635 refRightBottom = Position::kLeftBottom;
637 else if( rRightBottom == "rightbottom" )
639 right = xPos + width - boxWidth;
640 bottom = yPos + height - boxHeight;
641 refRightBottom = Position::kRightBottom;
644 return Position( left, top, right, bottom, rBox, refLeftTop,
649 Bezier *Builder::getPoints( const char *pTag ) const
651 vector<float> xBez, yBez;
655 if( sscanf( pTag, "(%d,%d)%n", &x, &y, &n ) < 2 )
662 "Slider points cannot have negative coordinates!" );
672 if( *(pTag++) != ',' )
678 // Create the Bezier curve
679 return new Bezier( getIntf(), xBez, yBez );