
///////////////////////////////////////////////
//											 //
// All files in X-framework are Copyright    //
// of Aleksandras Gluchovas 				 //
// 											 //
// e-mail: alex@soften.ktu.lt                //
//											 //
///////////////////////////////////////////////

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

/*** Implementation for class XCellBackground ***/

XCellBackground::XCellBackground( const wxColour& bkColor,
								  const wxColour& majorLineColor,
								  const wxColour& minorLineColor,
								  PosT  cellSize,
								  bool  showMinorLines,
								  bool  showRulers
				 			    )

	: mBkBrush   ( bkColor,           wxSOLID ),
	  mDarkPen   ( majorLineColor, 1, wxSOLID ),
	  mLightPen  ( minorLineColor, 1, wxSOLID ),

	  mSize      ( cellSize ),
	  mShowMinor ( showMinorLines ),
	  mShowRulers( showRulers )
{}

void XCellBackground::DrawHashes( wxDC&   canvas, 
								  XPoint& from, 
								  XRect&  bounds )
{
	PosT right  = bounds.x + bounds.width;
	PosT bottom = bounds.y + bounds.height;

	// draw vertical hashes

	while( from.x < right )
	{
		canvas.DrawLine( from.x, bounds.y, from.x, bottom );

		from.x += mSize;
	}

	// draw horizontal hashes

	while( from.y < bottom )
	{
		canvas.DrawLine( bounds.x, from.y, right, from.y );

		from.y += mSize;
	}
}

void XCellBackground::Draw( wxDC& canvas )
{
	XRect area = XObject::GetUpdateRegion( canvas );

	canvas.SetBrush( mBkBrush );
	canvas.SetPen  ( *wxTRANSPARENT_PEN );

	canvas.DrawRectangle( area.x, area.y, area.width, area.height );

	XPoint ubound;

	ubound.x = (int(area.x) / int(mSize) )*mSize;
	ubound.y = (int(area.y) / int(mSize) )*mSize;

	ubound.x -= mSize;
	ubound.y -= mSize;

	if ( mShowMinor )
	{
		XPoint tmp = ubound;

		// mintor lines start "later"
		tmp.x += int(mSize)/2;
		tmp.y += int(mSize)/2;

		canvas.SetPen( mLightPen );
		DrawHashes( canvas, tmp, area );
	}

	canvas.SetPen( mDarkPen );
	DrawHashes( canvas, ubound, area );
}

XRect XCellBackground::GetBounds()
{
	return XRect( -32760,-32760, 65530, 65530 );
}
