#include "stdafx.h"
#include "MapWidget.h"

MapWidget::MapWidget()
{
	title = L"";
}

MapWidget::~MapWidget()
{

}

int MapWidget::Create(HWND parent)
{
	hWndParent = parent;

	hWnd = CreateWindowEx(0,_T("SkinnableWindow"), title.data(), WS_CHILD|WS_CLIPSIBLINGS,
		location.X, location.Y, size.Width, size.Height, hWndParent, NULL, env.hInstance, NULL);

	if (!hWnd)
	{
		PrintLastError("MapWidget.Create");
		return 0;
	}

	// save the ptr to this class in the window structure
	SetWindowLongPtr(hWnd,GWLP_USERDATA,(LONG_PTR)this);

	if(hidden)
		ShowWindow(hWnd,SW_HIDE);
	else
		ShowWindow(hWnd, SW_SHOWNOACTIVATE);
	UpdateWindow(hWnd);
	return 1;
}

void MapWidget::Load(const char * base)
{
	visual.Load(base);
	SetLocation(visual.location.X,visual.location.Y);
	SetSize(visual.size.Width,visual.size.Height);
}

void MapWidget::Draw(Graphics & graphics)
{
	// intentionally left blank
}

void MapWidget::DrawToMap(Graphics& graphics)
{
	try {
		Point p = origin+visual.location;

		// Create a Rect object.
		Rect fillRect(p,visual.size);

		
		if(visual.current->bgcolor.GetAlpha()>0) {

			// Create a SolidBrush object.
			SolidBrush bgbrush(visual.current->bgcolor);

			// Fill the rectangle.
			graphics.FillRectangle(&bgbrush, fillRect);

		} 

		if(visual.current->image){
			DrawTransparentImage(&graphics,
				visual.current->image,
				p.X+visual.current->imagex,
				p.Y+visual.current->imagey,
				visual.size.Width,visual.size.Height);
		}

		if(visual.current->borderwidth>0){
			// Create a Pen object.
			Pen borderpen(visual.current->bordercolor, visual.current->borderwidth);

			// Draw the rectangle that bounds the image.
			fillRect.Width--;
			fillRect.Height--;
			graphics.DrawRectangle(&borderpen, fillRect);
		}

		if(visual.current->drawtext){
			// draw the text
			FontFamily   fontFamily(visual.fontname.data());
			Font         font(&fontFamily, visual.fontsize, FontStyleRegular, UnitPoint);
			SolidBrush brush(visual.current->textcolor);
			PointF		origin(p.X+visual.textlocation.X,
								p.Y+visual.textlocation.Y);
			StringFormat stringFormat;
			// left-justify each line of text.
			stringFormat.SetAlignment(StringAlignmentCenter);
			stringFormat.SetLineAlignment(StringAlignmentCenter);
			graphics.DrawString(title.data(), -1, &font, origin, &stringFormat, &brush);
		}
	} catch(...) {
		print(L"MapWidget::DrawToMap: Error drawing to map.\n");
		return;
	}
}

// pass these on to the parent
void MapWidget::OnMouseMove()
{
	SkinnableWindow::OnMouseMove();

	ImageView * wnd = (ImageView*)GetWindowLongPtr(theApp.mapwindow.mapview.hWnd,GWLP_USERDATA);
	wnd->OnMouseMove();
}

void MapWidget::OnLButtonDown()
{
	SkinnableWindow::OnLButtonDown();
	ImageView * wnd = (ImageView*)GetWindowLongPtr(theApp.mapwindow.mapview.hWnd,GWLP_USERDATA);
	wnd->OnLButtonDown();
}

void MapWidget::OnLButtonUp()
{
	SkinnableWindow::OnLButtonUp();
	ImageView * wnd = (ImageView*)GetWindowLongPtr(theApp.mapwindow.mapview.hWnd,GWLP_USERDATA);
	SafeSendMessage(theApp.mapwindow.hWnd,WM_COMMAND,MAKEWPARAM(0,ID_LEFTBUTTON),(LPARAM)hWnd);
	wnd->OnLButtonUp();
}

void MapWidget::SetState(AppearanceState s)
{
	if(s!=visual.state){
		visual.SetState(s);
		if(!hidden){
			theApp.mapwindow.UpdateMap();
		}
	}
}

void MapWidget::Set(Appearance &x)
{
	visual.fontname = x.fontname;
	visual.fontsize = x.fontsize;
	visual.size = x.size;
	visual.textlocation = x.textlocation;
	visual.normal = x.normal;
	visual.selected = x.selected;
	visual.mouseover = x.mouseover;
	visual.pressed = x.pressed;
	SkinnableWindow::SetSize(visual.size.Width,visual.size.Height);
}

void MapWidget::SetLocation(int x, int y)
{
	SkinnableWindow::SetLocation(x,y);
	visual.location = Point(x,y);
}

void MapWidget::SetSize(int width, int height)
{
	visual.size = Size(width,height);
	SkinnableWindow::SetSize(width,height);
}

bool MapWidget::Selected()
{
	return visual.state==AppearanceState::selected;
}