
#ifndef __tbitmapview_h__
#include "TBitmapView.h"
#endif


TBitmapView::TBitmapView(XGView *parent, XGArgStream &stream) : 
	XGView(parent,stream)
{
	fStretchBitmap = stream.ReadBoolean();
}

TBitmapView::~TBitmapView()
{
//	SetBitmap( 0, -1, -1, false );
}

void TBitmapView::DoDrawView( XRect /* inval */ )
{
	XRect cont = GetContentRect();
	int w = this->GetWidth();
	int h = this->GetHeight();
	int x, y;
	
	if (!w || !h) return;
	
	if (!fStretchBitmap) {
		if (w >= cont.right - cont.left)
		{
			w = cont.right - cont.left;
			x = cont.left;
		}
		else
		{
			x = cont.left + ((cont.right - cont.left) - w) / 2;
		}
		
		if (h >= cont.bottom - cont.top)
		{
			h = cont.bottom - cont.top;
			y = cont.top;
		}
		else
		{
			y = cont.top + ((cont.bottom - cont.top) - h) / 2;
		}
	} else {
		int cw = cont.right - cont.left;
		int ch = cont.bottom - cont.top;
		if (w * ch < h * cw) {	// height scale is greater than width scale
			// scale to fit width
			h = (h * cw) / w;
			w = cw;
		} else {
			// scale to fit height
			w = (w * ch) / h;
			h = ch;
		}
		x = cont.left + (cw - w) / 2;
		y = cont.top + (ch - h) / 2;
	}
	
	XGDraw draw( this );
	draw.DrawBitmap( this, x, y, w, h );
}
