skin_parser.cpp 9.82 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * skin_parser.cpp
 *****************************************************************************
 * Copyright (C) 2004 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Authors: Cyril Deguet     <asmax@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "skin_parser.hpp"
#include <math.h>

// Current DTD version
28
#define SKINS_DTD_VERSION "2.0"
29 30 31 32 33 34 35 36 37 38 39 40 41 42


SkinParser::SkinParser( intf_thread_t *pIntf, const string &rFileName ):
    XMLParser( pIntf, rFileName ), m_xOffset( 0 ), m_yOffset( 0 )
{
}


void SkinParser::handleBeginElement( const string &rName, AttrList_t &attr )
{
    if( rName == "Anchor" )
    {
        const BuilderData::Anchor anchor( atoi( attr["x"] ) + m_xOffset,
                atoi( attr["y"] ) + m_yOffset, atoi( attr["range"] ),
43
                atoi( attr["priority"] ), attr["points"], m_curWindowId );
44 45 46 47 48
        m_data.m_listAnchor.push_back( anchor );
    }

    else if( rName == "Bitmap" )
    {
49
        const BuilderData::Bitmap bitmap( attr["id"] , attr["file"],
50 51 52 53 54 55
                ConvertColor( attr["alphacolor"] ) );
        m_data.m_listBitmap.push_back( bitmap );
    }

    else if( rName == "Button" )
    {
56
        const BuilderData::Button button( uniqueId( attr["id"] ), atoi( attr["x"] ) +
57
                m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
58 59 60
                attr["rightbottom"], attr["visible"], attr["up"], attr["down"],
                attr["over"], attr["action"], attr["tooltiptext"], attr["help"],
                m_curLayer, m_curWindowId, m_curLayoutId );
61 62 63 64
        m_curLayer++;
        m_data.m_listButton.push_back( button );
    }

65
    else if( rName == "Checkbox" )
66
    {
67
        const BuilderData::Checkbox checkbox( uniqueId( attr["id"] ), atoi( attr["x"] ) +
68
                m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
69
                attr["rightbottom"], attr["visible"], attr["up1"], attr["down1"], attr["over1"],
70 71 72 73 74 75 76 77 78 79
                attr["up2"], attr["down2"], attr["over2"], attr["state"],
                attr["action1"], attr["action2"], attr["tooltiptext1"],
                attr["tooltiptext2"], attr["help"], m_curLayer, m_curWindowId,
                m_curLayoutId );
        m_curLayer++;
        m_data.m_listCheckbox.push_back( checkbox );
    }

    else if( rName == "Font" )
    {
80
        const BuilderData::Font fontData( attr["id"], attr["file"],
81 82 83 84 85 86 87 88 89 90 91 92 93 94
                atoi( attr["size"] ) );
        m_data.m_listFont.push_back( fontData );
    }

    else if( rName == "Group" )
    {
        m_xOffset += atoi( attr["x"] );
        m_yOffset += atoi( attr["y"] );
        m_xOffsetList.push_back( atoi( attr["x"] ) );
        m_yOffsetList.push_back( atoi( attr["y"] ) );
    }

    else if( rName == "Image" )
    {
95
        const BuilderData::Image imageData( uniqueId( attr["id"] ), atoi( attr["x"] ) +
96
                m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["lefttop"],
97
                attr["rightbottom"], attr["visible"],
98
                attr["image"], attr["action"], attr["help"], m_curLayer,
99 100 101 102 103 104 105
                m_curWindowId, m_curLayoutId );
        m_curLayer++;
        m_data.m_listImage.push_back( imageData );
    }

    else if( rName == "Layout" )
    {
106 107
        m_curLayoutId = uniqueId( attr["id"] );
        const BuilderData::Layout layout( m_curLayoutId, atoi( attr["width"] ),
108 109 110 111 112 113 114 115 116
                atoi( attr["height"] ), atoi( attr["minwidth"] ),
                atoi( attr["maxwidth"] ), atoi( attr["minheight"] ),
                atoi( attr["maxheight"] ), m_curWindowId );
        m_data.m_listLayout.push_back( layout );
        m_curLayer = 0;
    }

    else if( rName == "Playlist" )
    {
117 118
        m_curListId = uniqueId( attr["id"] );
        const BuilderData::List listData( m_curListId, atoi( attr["x"] ) +
119 120 121
                m_xOffset, atoi( attr["y"] ) + m_yOffset, attr["visible"],
                atoi( attr["width"]), atoi( attr["height"] ),
                attr["lefttop"], attr["rightbottom"],
122 123 124 125 126 127 128 129 130 131 132 133
                attr["font"], attr["var"], ConvertColor( attr["fgcolor"] ),
                ConvertColor( attr["playcolor"] ),
                ConvertColor( attr["bgcolor1"] ),
                ConvertColor( attr["bgcolor2"] ),
                ConvertColor( attr["selcolor"] ), attr["help"],
                m_curLayer, m_curWindowId, m_curLayoutId );
        m_curLayer++;
        m_data.m_listList.push_back( listData );
    }

    else if( rName == "RadialSlider" )
    {
134 135
        const BuilderData::RadialSlider radial( uniqueId( attr["id"] ),
                attr["visible"],
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
                attr["lefttop"], attr["rightbottom"], attr["sequence"],
                atoi( attr["nbImages"] ), atof( attr["minAngle"] ) * M_PI / 180,
                atof( attr["maxAngle"] ) * M_PI / 180, attr["value"],
                attr["tooltiptext"], attr["help"], m_curLayer, m_curWindowId,
                m_curLayoutId );
        m_curLayer++;
        m_data.m_listRadialSlider.push_back( radial );
    }

    else if( rName == "Slider" )
    {
        string newValue = attr["value"];
        if( m_curListId != "" )
        {
            // Slider associated to a list
            newValue = "playlist.slider";
        }
154 155
        const BuilderData::Slider slider( uniqueId( attr["id"] ),
                attr["visible"],
156 157 158 159 160 161 162 163 164 165 166
                atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
                attr["lefttop"], attr["rightbottom"], attr["up"], attr["down"],
                attr["over"], attr["points"], atoi( attr["thickness"] ),
                newValue, attr["tooltiptext"], attr["help"], m_curLayer,
                m_curWindowId, m_curLayoutId );
        m_curLayer++;
        m_data.m_listSlider.push_back( slider );
    }

    else if( rName == "Text" )
    {
167 168 169
        const BuilderData::Text textData( uniqueId( attr["id"] ),
                atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
                attr["visible"], attr["font"],
170 171
                attr["text"], atoi( attr["width"] ),
                ConvertColor( attr["color"] ), attr["help"], m_curLayer,
172 173 174 175 176 177 178 179 180 181 182 183
                m_curWindowId, m_curLayoutId );
        m_curLayer++;
        m_data.m_listText.push_back( textData );
    }

    else if( rName == "Theme" )
    {
        // Check the version
        if( strcmp( attr["version"], SKINS_DTD_VERSION ) )
        {
            msg_Err( getIntf(), "Bad theme version : %s (you need version %s)",
                     attr["version"], SKINS_DTD_VERSION );
184 185
            m_errors = true;
            return;
186
        }
187 188 189
        const BuilderData::Theme theme( attr["tooltipfont"],
                atoi( attr["magnet"] ), atoi( attr["alpha"] ),
                atoi( attr["movealpha"] ), atoi( attr["fadetime"] ) );
190 191 192 193 194 195 196 197 198
        m_data.m_listTheme.push_back( theme );
    }

    else if( rName == "ThemeInfo" )
    {
        msg_Warn( getIntf(), "skin: %s  author: %s", attr["name"],
                  attr["author"] );
    }

199 200 201 202 203 204
    else if( rName == "Video" )
    {
        const BuilderData::Video videoData( uniqueId( attr["id"] ),
                atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
                atoi( attr["width"] ), atoi( attr["height" ]),
                attr["lefttop"], attr["rightbottom"],
205
                attr["visible"], attr["help"], m_curLayer,
206 207 208 209 210
                m_curWindowId, m_curLayoutId );
        m_curLayer++;
        m_data.m_listVideo.push_back( videoData );
    }

211 212
    else if( rName == "Window" )
    {
213 214
        m_curWindowId = uniqueId( attr["id"] );
        const BuilderData::Window window( m_curWindowId,
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
                atoi( attr["x"] ) + m_xOffset, atoi( attr["y"] ) + m_yOffset,
                ConvertBoolean( attr["visible"] ),
                ConvertBoolean( attr["dragdrop"] ),
                ConvertBoolean( attr["playondrop"] ) );
        m_data.m_listWindow.push_back( window );
    }
}


void SkinParser::handleEndElement( const string &rName )
{
    if( rName == "Group" )
    {
        m_xOffset -= m_xOffsetList.back();
        m_yOffset -= m_yOffsetList.back();
        m_xOffsetList.pop_back();
        m_yOffsetList.pop_back();
    }

    else if( rName == "Playlist" )
    {
        m_curListId = "";
    }
}


bool SkinParser::ConvertBoolean( const char *value ) const
{
    return strcmp( value, "true" ) == 0;
}


int SkinParser::ConvertColor( const char *transcolor ) const
{
    unsigned long iRed, iGreen, iBlue;
    iRed = iGreen = iBlue = 0;
    sscanf( transcolor, "#%2lX%2lX%2lX", &iRed, &iGreen, &iBlue );
    return ( iRed << 16 | iGreen << 8 | iBlue );
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
const string SkinParser::generateId() const
{
    static int i = 1;

    char genId[5];
    snprintf( genId, 4, "%i", i++ );

    string base = "_ReservedId_" + (string)genId;

    return base;
}


const string SkinParser::uniqueId( const string &id )
{
    string newId;

    if( m_idSet.find( id ) != m_idSet.end() )
    {
        // The id was already used
        if( id != "none" )
        {
            msg_Warn( getIntf(), "Non unique id: %s", id.c_str() );
        }
        newId = generateId();
    }
    else
    {
        // OK, this is a new id
        newId = id;
    }

    // Add the id to the set
    m_idSet.insert( newId );

    return newId;
}