/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     Contrib. demo
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     2000/02/08
// RCS-ID:      $Id: cardglyph.cpp,v 1.3 2001/04/04 12:42:36 aleksandras Exp $
// Copyright:   (c) Aleksandras Gluchovas (alex@soften.ktu.lt)
// Licence:   	GPL (version 2.0 or later)
/////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
    #pragma implementation "minimal.cpp"
    #pragma interface "minimal.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 "cardglyph.h"

/***** Implementation for class CardCtrlNode *****/

IMPLEMENT_DYNAMIC_CLASS( CardCtrlNode, MultiFaceNodeBase )

CardCtrlNode::CardCtrlNode()
{}

wxDMNode* CardCtrlNode::Clone( bool uniqueCopy, bool cloneChildren )
{
	CardCtrlNode* pCopy = new CardCtrlNode();

	AssignData( pCopy, uniqueCopy, cloneChildren );

	return pCopy;
}

/***** Implementation for class CardCtrlGlyph *****/

IMPLEMENT_DYNAMIC_CLASS( CardCtrlGlyph, MultiFaceGlyphBase )

CardCtrlGlyph::CardCtrlGlyph()
{}

#define CELL_SIZE   10
#define CELL_MARGIN 1

void CardCtrlGlyph::Draw( wxDC& canvas)
{
	size_t cnt = mpNode->GetChildCount();

	int w = cnt * CELL_SIZE, h = CELL_SIZE;
	int x1 = mPixBounds.x + mPixBounds.width  - w - CELL_MARGIN;
	int y1 = mPixBounds.y + mPixBounds.height - h - CELL_MARGIN;

	wxDC& dc = canvas;

	// fill bottom-area

	wxBrush bkbr( wxColour(192,192,192), wxSOLID );
	dc.SetBrush( bkbr );
	dc.SetPen( *wxTRANSPARENT_PEN );
	dc.DrawRectangle( mPixBounds.x, 
					  mPixBounds.y + mPixBounds.height - CELL_SIZE - CELL_MARGIN, 
					  mPixBounds.width, 
					  CELL_SIZE + CELL_MARGIN );

	// draw card-selector

	dc.SetPen( *wxBLACK_PEN );
	dc.SetBrush( *wxWHITE_BRUSH );

	dc.DrawRectangle( x1,y1, w,h );
	dc.SetBrush( *wxBLACK_BRUSH );

	// draw dividers between cells
	size_t i = 0;
	for( ; i != cnt; ++ i )
	{
		int x = x1 + i * CELL_SIZE + CELL_SIZE;
		dc.DrawLine( x, y1, x, y1 + CELL_SIZE );

		if ( i == GetVisibleFace() )
		
			dc.DrawRectangle( x + 3 - CELL_SIZE, y1 + 3, 
							  CELL_SIZE - 6, CELL_SIZE - 6 );
	}

	// draw outline

	wxPen p1( *wxBLACK, 1, wxLONG_DASH );
	dc.SetPen( p1 );
	XRect& r = mPixBounds;

	dc.DrawLine( r.x, r.y, r.x + r.width, r.y );
	dc.DrawLine( r.x + r.width, r.y, r.x + r.width, r.y + r.height );
	dc.DrawLine( r.x + r.width, r.y + r.height, r.x, r.y + r.height );
	dc.DrawLine( r.x, r.y + r.height, r.x, r.y );
}

bool CardCtrlGlyph::OnMouseDown( const XPoint& pos )
{
	size_t cnt = mpNode->GetChildCount();
	int x1 = mPixBounds.x + mPixBounds.width - cnt * CELL_SIZE - CELL_MARGIN;

	if ( pos.x < x1 || cnt == 0 ) 
		
		return FALSE; // event not handled

	int pg = ( pos.x - x1 ) / CELL_SIZE;

	SetVisibleFace( pg >= (int)cnt ? (int)cnt - 1 : pg );

	return TRUE; 
}