]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/parser/builder.cpp
* skins2: Text control now accepts lefttop and rightbottom attributes, like
[vlc] / modules / gui / skins2 / parser / builder.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 2e6e6be..ba1331a
@@ -29,9 +29,9 @@
 #include "../src/png_bitmap.hpp"
 #include "../src/os_factory.hpp"
 #include "../src/generic_bitmap.hpp"
-#include "../src/generic_window.hpp"
-#include "../src/vout_window.hpp"
+#include "../src/top_window.hpp"
 #include "../src/anchor.hpp"
+#include "../src/bitmap_font.hpp"
 #include "../src/ft2_font.hpp"
 #include "../src/theme.hpp"
 #include "../controls/ctrl_button.hpp"
 #include "../controls/ctrl_slider.hpp"
 #include "../controls/ctrl_radialslider.hpp"
 #include "../controls/ctrl_text.hpp"
+#include "../controls/ctrl_video.hpp"
 #include "../utils/position.hpp"
 #include "../utils/var_bool.hpp"
 #include "../utils/var_text.hpp"
 
+#include "vlc_image.h"
 
-Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData):
+
+Builder::Builder( intf_thread_t *pIntf, const BuilderData &rData ):
     SkinObject( pIntf ), m_rData( rData ), m_pTheme( NULL )
 {
+    m_pImageHandler = image_HandlerCreate( pIntf );
 }
 
+Builder::~Builder()
+{
+    if( m_pImageHandler ) image_HandlerDelete( m_pImageHandler );
+}
 
 CmdGeneric *Builder::parseAction( const string &rAction )
 {
@@ -81,6 +89,7 @@ Theme *Builder::build()
     // Create everything from the data in the XML
     ADD_OBJECTS( Theme );
     ADD_OBJECTS( Bitmap );
+    ADD_OBJECTS( BitmapFont );
     ADD_OBJECTS( Font );
     ADD_OBJECTS( Window );
     ADD_OBJECTS( Layout );
@@ -102,7 +111,7 @@ Theme *Builder::build()
 #define GET_BMP( pBmp, id ) \
     if( id != "none" ) \
     { \
-        pBmp = m_pTheme->m_bitmaps[id].get(); \
+        pBmp = m_pTheme->getBitmapById(id); \
         if( pBmp == NULL ) \
         { \
             msg_Err( getIntf(), "unknown bitmap id: %s", id.c_str() ); \
@@ -116,24 +125,49 @@ void Builder::addTheme( const BuilderData::Theme &rData )
     rManager.setMagnetValue( rData.m_magnet );
     rManager.setAlphaValue( rData.m_alpha );
     rManager.setMoveAlphaValue( rData.m_moveAlpha );
-    // XXX:  font to fix
-    GenericFont *pFont = new FT2Font( getIntf(), "FreeSans.ttf", 12 );
-    pFont->init();
-    rManager.createTooltip( *pFont );
+    GenericFont *pFont = getFont( rData.m_tooltipfont );
+    if( pFont )
+    {
+        rManager.createTooltip( *pFont );
+    }
+    else
+    {
+        msg_Warn( getIntf(), "Invalid tooltip font: %s",
+                  rData.m_tooltipfont.c_str() );
+    }
 }
 
 
 void Builder::addBitmap( const BuilderData::Bitmap &rData )
 {
-    GenericBitmap *pBmp = new PngBitmap( getIntf(), rData.m_fileName,
-                                         rData.m_alphaColor );
+    GenericBitmap *pBmp =
+        new PngBitmap( getIntf(), m_pImageHandler,
+                       rData.m_fileName, rData.m_alphaColor );
+    m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
+}
+
+
+void Builder::addBitmapFont( const BuilderData::BitmapFont &rData )
+{
+    GenericBitmap *pBmp =
+        new PngBitmap( getIntf(), m_pImageHandler, rData.m_file, 0 );
     m_pTheme->m_bitmaps[rData.m_id] = GenericBitmapPtr( pBmp );
+
+    GenericFont *pFont = new BitmapFont( getIntf(), *pBmp, rData.m_type );
+    if( pFont->init() )
+    {
+        m_pTheme->m_fonts[rData.m_id] = GenericFontPtr( pFont );
+    }
+    else
+    {
+        delete pFont;
+    }
 }
 
 
 void Builder::addFont( const BuilderData::Font &rData )
 {
-    GenericFont *pFont = new FT2Font( getIntf(), rData.m_fontName,
+    GenericFont *pFont = new FT2Font( getIntf(), rData.m_fontFile,
                                       rData.m_size );
     if( pFont->init() )
     {
@@ -148,18 +182,18 @@ void Builder::addFont( const BuilderData::Font &rData )
 
 void Builder::addWindow( const BuilderData::Window &rData )
 {
-    GenericWindow *pWin =
-        new GenericWindow( getIntf(), rData.m_xPos, rData.m_yPos,
+    TopWindow *pWin =
+        new TopWindow( getIntf(), rData.m_xPos, rData.m_yPos,
                            m_pTheme->getWindowManager(),
                            rData.m_dragDrop, rData.m_playOnDrop );
 
-    m_pTheme->m_windows[rData.m_id] = GenericWindowPtr( pWin );
+    m_pTheme->m_windows[rData.m_id] = TopWindowPtr( pWin );
 }
 
 
 void Builder::addLayout( const BuilderData::Layout &rData )
 {
-    GenericWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
+    TopWindow *pWin = m_pTheme->getWindowById(rData.m_windowId);
     if( pWin == NULL )
     {
         msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
@@ -179,16 +213,16 @@ void Builder::addLayout( const BuilderData::Layout &rData )
     m_pTheme->m_layouts[rData.m_id] = GenericLayoutPtr( pLayout );
 
     // Attach the layout to its window
-    pWin->setActiveLayout( pLayout );
+    m_pTheme->getWindowManager().addLayout( *pWin, *pLayout );
 }
 
 
 void Builder::addAnchor( const BuilderData::Anchor &rData )
 {
-    GenericWindow *pWin = m_pTheme->m_windows[rData.m_windowId].get();
-    if( pWin == NULL )
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
+    if( pLayout == NULL )
     {
-        msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
+        msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
         return;
     }
 
@@ -203,8 +237,8 @@ void Builder::addAnchor( const BuilderData::Anchor &rData )
 
     Anchor *pAnc = new Anchor( getIntf(), rData.m_xPos, rData.m_yPos,
                                rData.m_range, rData.m_priority,
-                               *pCurve, *pWin );
-    pWin->addAnchor( pAnc );
+                               *pCurve, *pLayout );
+    pLayout->addAnchor( pAnc );
 }
 
 
@@ -220,7 +254,7 @@ void Builder::addButton( const BuilderData::Button &rData )
     GenericBitmap *pBmpOver = pBmpUp;
     GET_BMP( pBmpOver, rData.m_overId );
 
-    GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
     if( pLayout == NULL )
     {
         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
@@ -234,9 +268,14 @@ void Builder::addButton( const BuilderData::Button &rData )
         return;
     }
 
+    // Get the visibility variable
+    // XXX check when it is null
+    Interpreter *pInterpreter = Interpreter::instance( getIntf() );
+    VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
+
     CtrlButton *pButton = new CtrlButton( getIntf(), *pBmpUp, *pBmpOver,
         *pBmpDown, *pCommand, UString( getIntf(), rData.m_tooltip.c_str() ),
-        UString( getIntf(), rData.m_help.c_str() ), NULL );
+        UString( getIntf(), rData.m_help.c_str() ), pVisible );
 
     // Compute the position of the control
     // XXX (we suppose all the images have the same size...)
@@ -272,7 +311,7 @@ void Builder::addCheckbox( const BuilderData::Checkbox &rData )
     GenericBitmap *pBmpOver2 = pBmpUp2;
     GET_BMP( pBmpOver2, rData.m_over2Id );
 
-    GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
     if( pLayout == NULL )
     {
         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
@@ -302,12 +341,16 @@ void Builder::addCheckbox( const BuilderData::Checkbox &rData )
         return;
     }
 
+    // Get the visibility variable
+    // XXX check when it is null
+    VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
+
     // Create the control
     CtrlCheckbox *pCheckbox = new CtrlCheckbox( getIntf(), *pBmpUp1,
         *pBmpOver1, *pBmpDown1, *pBmpUp2, *pBmpOver2, *pBmpDown2, *pCommand1,
         *pCommand2, UString( getIntf(), rData.m_tooltip1.c_str() ),
         UString( getIntf(), rData.m_tooltip2.c_str() ), *pVar,
-        UString( getIntf(), rData.m_help.c_str() ), NULL );
+        UString( getIntf(), rData.m_help.c_str() ), pVisible );
 
     // Compute the position of the control
     // XXX (we suppose all the images have the same size...)
@@ -327,23 +370,27 @@ void Builder::addImage( const BuilderData::Image &rData )
     GenericBitmap *pBmp = NULL;
     GET_BMP( pBmp, rData.m_bmpId );
 
-    GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
     if( pLayout == NULL )
     {
         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
         return;
     }
 
-    GenericWindow *pWindow = m_pTheme->m_windows[rData.m_windowId].get();
+    TopWindow *pWindow = m_pTheme->getWindowById(rData.m_windowId);
     if( pWindow == NULL )
     {
         msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
         return;
     }
 
+    // Get the visibility variable
+    // XXX check when it is null
+    Interpreter *pInterpreter = Interpreter::instance( getIntf() );
+    VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
+
     CtrlImage *pImage = new CtrlImage( getIntf(), *pBmp,
-                                UString( getIntf(), rData.m_help.c_str() ),
-                                NULL);
+        UString( getIntf(), rData.m_help.c_str() ), pVisible );
 
     // Compute the position of the control
     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
@@ -359,7 +406,7 @@ void Builder::addImage( const BuilderData::Image &rData )
              NULL);
         pLayout->addControl( pMove, pos, rData.m_layer );
     }
-    else if( rData.m_actionId == "resize" )
+    else if( rData.m_actionId == "resizeSE" )
     {
         CtrlResize *pResize = new CtrlResize( getIntf(), *pImage, *pLayout,
                 UString( getIntf(), rData.m_help.c_str() ), NULL );
@@ -376,14 +423,14 @@ void Builder::addImage( const BuilderData::Image &rData )
 
 void Builder::addText( const BuilderData::Text &rData )
 {
-    GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
     if( pLayout == NULL )
     {
         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
         return;
     }
 
-    GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
+    GenericFont *pFont = getFont( rData.m_fontId );
     if( pFont == NULL )
     {
         msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
@@ -396,15 +443,23 @@ void Builder::addText( const BuilderData::Text &rData )
     pVar->set( msg );
     m_pTheme->m_vars.push_back( VariablePtr( pVar ) );
 
+    // Get the visibility variable
+    // XXX check when it is null
+    Interpreter *pInterpreter = Interpreter::instance( getIntf() );
+    VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
+
     CtrlText *pText = new CtrlText( getIntf(), *pVar, *pFont,
-            UString( getIntf(), rData.m_help.c_str() ), rData.m_color, NULL );
+        UString( getIntf(), rData.m_help.c_str() ), rData.m_color, pVisible );
 
     int height = pFont->getSize();
 
-    pLayout->addControl( pText, Position( rData.m_xPos, rData.m_yPos,
-                                          rData.m_xPos + rData.m_width,
-                                          rData.m_yPos + height, *pLayout ),
-                         rData.m_layer );
+    // Compute the position of the control
+    const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
+                                       rData.m_xPos, rData.m_yPos,
+                                       rData.m_width, height,
+                                       *pLayout );
+
+    pLayout->addControl( pText, pos, rData.m_layer );
 
     m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pText );
 }
@@ -416,7 +471,7 @@ void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
     GenericBitmap *pSeq = NULL;
     GET_BMP( pSeq, rData.m_sequence );
 
-    GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
     if( pLayout == NULL )
     {
         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
@@ -432,12 +487,16 @@ void Builder::addRadialSlider( const BuilderData::RadialSlider &rData )
         return;
     }
 
+    // Get the visibility variable
+    // XXX check when it is null
+    VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
+
     // Create the control
     CtrlRadialSlider *pRadial =
         new CtrlRadialSlider( getIntf(), *pSeq, rData.m_nbImages, *pVar,
                               rData.m_minAngle, rData.m_maxAngle,
                               UString( getIntf(), rData.m_help.c_str() ),
-                              NULL );
+                              pVisible );
 
     // XXX: resizing is not supported
     // Compute the position of the control
@@ -464,7 +523,7 @@ void Builder::addSlider( const BuilderData::Slider &rData )
     GenericBitmap *pBmpOver = pBmpUp;
     GET_BMP( pBmpOver, rData.m_overId );
 
-    GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
     if( pLayout == NULL )
     {
         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
@@ -519,14 +578,14 @@ void Builder::addSlider( const BuilderData::Slider &rData )
 
 void Builder::addList( const BuilderData::List &rData )
 {
-    GenericLayout *pLayout = m_pTheme->m_layouts[rData.m_layoutId].get();
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
     if( pLayout == NULL )
     {
         msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
         return;
     }
 
-    GenericFont *pFont = m_pTheme->m_fonts[rData.m_fontId].get();
+    GenericFont *pFont = getFont( rData.m_fontId );
     if( pFont == NULL )
     {
         msg_Err( getIntf(), "Unknown font id: %s", rData.m_fontId.c_str() );
@@ -542,11 +601,15 @@ void Builder::addList( const BuilderData::List &rData )
         return;
     }
 
+    // Get the visibility variable
+    // XXX check when it is null
+    VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
+
     // Create the list control
     CtrlList *pList = new CtrlList( getIntf(), *pVar, *pFont,
        rData.m_fgColor, rData.m_playColor, rData.m_bgColor1,
        rData.m_bgColor2, rData.m_selColor,
-       UString( getIntf(), rData.m_help.c_str() ), NULL );
+       UString( getIntf(), rData.m_help.c_str() ), pVisible );
 
     // Compute the position of the control
     const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
@@ -562,17 +625,30 @@ void Builder::addList( const BuilderData::List &rData )
 
 void Builder::addVideo( const BuilderData::Video &rData )
 {
-    GenericWindow *pWindow = m_pTheme->m_windows[rData.m_windowId].get();
-    if( pWindow == NULL )
+    GenericLayout *pLayout = m_pTheme->getLayoutById(rData.m_layoutId);
+    if( pLayout == NULL )
     {
-        msg_Err( getIntf(), "unknown window id: %s", rData.m_windowId.c_str() );
+        msg_Err( getIntf(), "unknown layout id: %s", rData.m_layoutId.c_str() );
         return;
     }
 
-    VoutWindow *pVout = new VoutWindow( getIntf(), rData.m_xPos,
-            rData.m_yPos, m_pTheme->getWindowManager(), false, false,
-            *pWindow );
-    m_pTheme->m_vouts.push_back( VoutWindowPtr( pVout ) );
+    // Get the visibility variable
+    // XXX check when it is null
+    Interpreter *pInterpreter = Interpreter::instance( getIntf() );
+    VarBool *pVisible = pInterpreter->getVarBool( rData.m_visible, m_pTheme );
+
+    CtrlVideo *pVideo = new CtrlVideo( getIntf(),
+        UString( getIntf(), rData.m_help.c_str() ), pVisible );
+
+    // Compute the position of the control
+    const Position pos = makePosition( rData.m_leftTop, rData.m_rightBottom,
+                                       rData.m_xPos, rData.m_yPos,
+                                       rData.m_width, rData.m_height,
+                                       *pLayout );
+
+    pLayout->addControl( pVideo, pos, rData.m_layer );
+
+    m_pTheme->m_controls[rData.m_id] = CtrlGenericPtr( pVideo );
 }
 
 
@@ -645,6 +721,42 @@ const Position Builder::makePosition( const string &rLeftTop,
 }
 
 
+GenericFont *Builder::getFont( const string &fontId )
+{
+    GenericFont *pFont = m_pTheme->getFontById(fontId);
+    if( !pFont && fontId == "defaultfont" )
+    {
+        // Get the resource path and try to load the default font
+        OSFactory *pOSFactory = OSFactory::instance( getIntf() );
+        const list<string> &resPath = pOSFactory->getResourcePath();
+        const string &sep = pOSFactory->getDirSeparator();
+
+        list<string>::const_iterator it;
+        for( it = resPath.begin(); it != resPath.end(); it++ )
+        {
+            string path = (*it) + sep + "fonts" + sep + "FreeSans.ttf";
+            pFont = new FT2Font( getIntf(), path, 12 );
+            if( pFont->init() )
+            {
+                // Font loaded successfully
+                m_pTheme->m_fonts["defaultfont"] = GenericFontPtr( pFont );
+                break;
+            }
+            else
+            {
+                delete pFont;
+                pFont = NULL;
+            }
+        }
+        if( !pFont )
+        {
+            msg_Err( getIntf(), "Failed to open the default font" );
+        }
+    }
+    return pFont;
+}
+
+
 Bezier *Builder::getPoints( const char *pTag ) const
 {
     vector<float> xBez, yBez;