/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     Contrib. demo
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     2000/02/10
// RCS-ID:      
// Copyright:   (c) PM-Soft
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
    #pragma implementation "cardctrl.cpp"
    #pragma interface "cardctrl.cpp"
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "wx/wspp/cardctrl.h"
#include "wx/list.h"

#include "wx/log.h"

/***** Implementation for class wxCardCtrl *****/

IMPLEMENT_DYNAMIC_CLASS( wxCardCtrl, wxPanel )

BEGIN_EVENT_TABLE( wxCardCtrl, wxPanel )

END_EVENT_TABLE()

wxCardCtrl::wxCardCtrl()
	: mShownId(-1),
	  wxPanel()
{}

wxCardCtrl::wxCardCtrl( wxWindow* parent, wxWindowID id, 
						const wxPoint& pos, const wxSize& size, 
						long style, const wxString& name )

	: mShownId(-1),
	  wxPanel( parent, id, pos, size, style, name )
{}

bool wxCardCtrl::Create( wxWindow* parent, wxWindowID id, 
						 const wxPoint& pos, const wxSize& size, 
						 long style, const wxString& name )
{
	return wxPanel::Create( parent, id, pos, size, style, name );
}

bool wxCardCtrl::ShowCard( wxWindowID cardWndId )
{
	mShownId = cardWndId;
	return ArrangeCards();
}

bool wxCardCtrl::CardIsShown( wxWindowID cardWndId )
{
	wxWindow* pChild = FindChildById( cardWndId );

	if ( !pChild )

		return FALSE;
	else
		return pChild->IsShown();
}

/*** protected methods ***/

wxWindow* wxCardCtrl::FindChildById( wxWindowID id )
{
	wxNode* pNode = this->GetChildren().First();

	while( pNode )
	{
		wxWindow* pChild = (wxWindow*)pNode->GetData();

		if ( pChild->GetId() == id )

			return pChild;

		pNode = pNode->GetNext();
	}

	return NULL;
}

bool wxCardCtrl::ArrangeCards()
{
	wxNode* pNode = this->GetChildren().First();
	wxWindow* pActive = NULL;

	// hide not active cards

	while( pNode )
	{
		wxWindow* pChild = (wxWindow*)pNode->GetData();
		wxWindowID id = pChild->GetId();

		if ( id != mShownId )
		{
			if ( pChild->IsShown() )

				pChild->Show(FALSE);
		}
		else
			pActive = pChild;

		pNode = pNode->GetNext();
	}

	if ( !pActive ) 
	{
		wxLogWarning( "wxCardCtrl::ArrangeCards - active card not found!" );
		return FALSE;
	}

	// do resizing before the card is shown

	int w = 0, h = 0, cw = 0, ch = 0;
	pActive->GetSize( &w, &h );

	GetClientSize( &cw, &ch );
	
	if ( cw != w || ch != h )

		pActive->SetSize( 0,0, cw, ch );

	// show it

	if ( !pActive->IsShown() )

		pActive->Show();

	return TRUE;
}