/*
 * pawscheckbox.cpp
 *
 * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
 *
 *
 * 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 (version 2 of the License)
 * 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-1307, USA.
 *
 */
#include <psconfig.h>

#include "pawscheckbox.h"
#include "pawsmanager.h"
#include "pawstexturemanager.h"
#include "pawstextbox.h"

//---------------------------------------------------------------------------------
pawsCheckBox::pawsCheckBox()
{
    checkBoxOff = "radiooff";
    checkBoxOn  = "radioon";
    checkBoxSize = 16;
}

void pawsCheckBox::SetState( bool state )
{    
    checkBox->SetState( state );
}

pawsCheckBox::~pawsCheckBox()
{
}

bool pawsCheckBox::Setup( iDocumentNode* node )
{
    csRef<iDocumentNode> textNode = node->GetNode( "text" );        
    
    if ( !textNode )
    {
        Error2("%s XML is defined incorrectly. No <text> tag found", name.GetData());
        return false;
    }
    
    csString pos(textNode->GetAttributeValue("position"));

    
    ///////////////////////////////////////////////////////////////////////
    // Create the check box
    ///////////////////////////////////////////////////////////////////////
    checkBox = (pawsButton*)PawsManager::GetSingleton().CreateWidget("pawsButton");
    AddChild( checkBox );

    csRect boxRect;
    csRect textRect;
    
    if ( pos == "left" )    
    {
        boxRect = csRect(  defaultFrame.Width() - GetActualWidth(checkBoxSize), 4,
            defaultFrame.Width(), GetActualHeight(checkBoxSize) + 4 );

        textRect   = csRect(  0, 4, defaultFrame.Width() - GetActualWidth(checkBoxSize), defaultFrame.Height() );
    }
    else
    {
        boxRect = csRect(  4, 4, 
            GetActualWidth(checkBoxSize) + 4, GetActualHeight(checkBoxSize) + 4 );

        textRect   = csRect(  4 + GetActualWidth(checkBoxSize) + 2, 4, defaultFrame.Width(), defaultFrame.Height() );
    }


    csRef<iDocumentNode> checkBoxNode = node->GetNode( "checkbox" );
                
    if ( checkBoxNode )
    {
        csRef<iDocumentAttribute> attr;

        attr = checkBoxNode->GetAttribute("off");
        if (attr)
            checkBoxOff = attr->GetValue();

        attr = checkBoxNode->GetAttribute("on");
        if (attr)
            checkBoxOn  = attr->GetValue();

        attr = checkBoxNode->GetAttribute("size");
        if (attr)
            checkBoxSize     = attr->GetValueAsInt();
    }
        
    checkBox->SetRelativeFrame( boxRect.xmin, boxRect.ymin, 
                                checkBoxSize, checkBoxSize );
    checkBox->SetUpImage( checkBoxOff );
    checkBox->SetDownImage( checkBoxOn );
    checkBox->SetState( false );
    checkBox->SetToggle( true );
    checkBox->PostSetup();
    checkBox->SetID( id );
     
    
    ///////////////////////////////////////////////////////////////////////
    // Create the textbox that has the current selected choice
    ///////////////////////////////////////////////////////////////////////    
    csString str(textNode->GetAttributeValue("string"));
    
    text = (pawsTextBox*)PawsManager::GetSingleton().CreateWidget("pawsTextBox");
    AddChild( text );

    // Puts the box at the edge of the text box widget
    text->SetRelativeFrame( textRect.xmin, textRect.ymin,
                            textRect.Width(), textRect.Height() );
    text->PostSetup();
    
    text->SetText(str);
    text->SetID( id );

    return true;               
}

bool pawsCheckBox::SelfPopulate( iDocumentNode *node)
{
    if (node->GetAttributeValue("text"))
    {
        checkBox->SetText (node->GetAttributeValue("text"));
    }

    if (node->GetAttributeValue("down"))
    {
        checkBox->SetState(strcmp(node->GetAttributeValue("down"),"true")==0);
    }

    return true;
}

bool pawsCheckBox::OnButtonPressed( int mouseButton, int keyModifier, pawsWidget* widget )
{
    if ( parent )
        return parent->OnButtonPressed( mouseButton, keyModifier, this );
    else
        return false;
}

bool pawsCheckBox::GetState()
{
    if (checkBox != NULL)
        return checkBox->GetState();
    else
        return false;
}

void pawsCheckBox::SetImages(const char* up, const char* down)
{
    checkBox->SetUpImage( up );
    checkBox->SetDownImage( down );
}

void pawsCheckBox::OnUpdateData(const char *dataname,PAWSData& value)
{
    // This is called automatically whenever subscribed data is published.
    if (checkBox)
    {
        checkBox->SetState( value.GetBool() );
    }
}
