/*
* Author: Andrew Mann
*
* Copyright (C) 2004 PlaneShift Team (info@planeshift.it,
* http://www.planeshift.it)
*
* 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 "pawscollapsablewidget.h"
#include "partview.h"


#include "paws/pawsmanager.h"
#include "paws/pawsbutton.h"
#include "paws/pawsborder.h"


pawsCollapsableWidget::pawsCollapsableWidget(PawsManager* manager )
            :pawsWidget( manager )
{
    collapsed=false;
    collapsed_title="--EXPAND--";
    expanded_title="--COLLAPSE--";
    expand_collapse_button=NULL;
    organize_children=false;
    recalced_this_frame=true;
}

pawsCollapsableWidget::~pawsCollapsableWidget()
{
}

bool pawsCollapsableWidget::Setup( iDocumentNode* node )
{
    csRef<iDocumentNode> titlenode,subnode;
    titlenode = node->GetNode( "title" );
    if ( titlenode )
    {
        subnode=titlenode->GetNode("expanded");
        if (subnode)
            SetExpandedTitle(subnode->GetAttributeValue("text"));
        subnode=titlenode->GetNode("collapsed");
        if (subnode)
            SetCollapsedTitle(subnode->GetAttributeValue("text"));
        subnode=titlenode->GetNode("button");
        if (subnode)
        {
            expand_collapse_button=(pawsButton *)windowManager->CreateWidget("pawsButton");
            if (!expand_collapse_button->Load(subnode))
                Error2("%s XML is defined incorrectly. Failed to parse <button> tag beneath <title> block", name.GetData());
        }
    }
    subnode = node->GetNode( "organizechildren" );
    if (subnode)
        organize_children=true;

    subnode = node->GetNode( "collapsed" );
    if (subnode)
        collapsed=true;
    


    return true;
}

bool pawsCollapsableWidget::PostSetup()
{
 
    if (!expand_collapse_button)
        expand_collapse_button=(pawsButton *)windowManager->CreateWidget("pawsButton");

    expand_collapse_button->SetText(expanded_title);
    expand_collapse_button->SetParent(this);
    expand_collapse_button->SetRelativeFrame(GetActualWidth(5),GetActualHeight(3),
        screenFrame.Width()-GetActualWidth(10),GetActualHeight(12));
    expand_collapse_button->Show();
    AddChild(expand_collapse_button);

    if (border)
        uncollapsed_height=border->GetRect().Height();
    else
        uncollapsed_height=screenFrame.Height();
 
    if (organize_children)
        OrganizeChildren();
    recalced_this_frame=false;


    if (collapsed)
        expand_collapse_button->SetText(collapsed_title);


    return true;
}


void pawsCollapsableWidget::DrawChildren()
{
    recalced_this_frame=false;

    if (!collapsed)
        pawsWidget::DrawChildren();
    else
    {
        if (expand_collapse_button) expand_collapse_button->Draw();
        if (border) border->Draw();
    }
}

void pawsCollapsableWidget::SetCollapsedTitle(const char *c_title)
{
    collapsed_title=c_title;
}

void pawsCollapsableWidget::SetExpandedTitle(const char *c_title)
{
    expanded_title=c_title;
}


csRect pawsCollapsableWidget::ScreenFrame()
{
    if (collapsed)
    {
        screenFrame.ymax=screenFrame.ymin+GetActualHeight(20);
        return screenFrame;
    }

    // Adjust our frame based on children if applicable
    if (organize_children && ! recalced_this_frame)
    {
        recalced_this_frame=true;
        OrganizeChildren();
    }
    return screenFrame;
}

void pawsCollapsableWidget::OrganizeChildren()
{
    int i,l,y;

    l=children.Length();

    y=20;
    for (i=0;i<l;i++)
    {
        csRect childframe;
        if (children[i] == expand_collapse_button)
            continue;
        
        if (children[i]->GetBorder())
            childframe=children[i]->GetBorder()->GetRect();
        else
            childframe=children[i]->ScreenFrame();

        children[i]->SetRelativeFramePos(childframe.xmin-screenFrame.xmin,y);
        y+=childframe.Height();
    }
    screenFrame.ymax=screenFrame.ymin+y+5;
}

void pawsCollapsableWidget::Collapse()
{
    if (!collapsed)
    {
        screenFrame.ymax=screenFrame.ymin+GetActualHeight(20);
        expand_collapse_button->SetText(collapsed_title);
        collapsed=true;
    }
}

void pawsCollapsableWidget::Expand()
{
    if (collapsed)
    {
        // Uncollapse
        if (organize_children)
            OrganizeChildren();
        else
            screenFrame.ymax=screenFrame.ymin+uncollapsed_height;
        expand_collapse_button->SetText(expanded_title);
        collapsed=false;
    }
}

bool pawsCollapsableWidget::OnButtonPressed( int button, int keyModifier, pawsWidget* widget )
{
    if (!widget)
        return false;
    if (widget == expand_collapse_button)
    {
        if (collapsed)
            Expand();
        else
            Collapse();

        return true;
    }

    if (parent)
        return parent->OnButtonPressed(button,keyModifier,widget);
    return false;
}

void pawsCollapsableWidget::BringToTop( pawsWidget* widget )
{
    if (!organize_children)
        pawsWidget::BringToTop(widget);
    // otherwise do nothing.  Do not reorder children.
}

