#include "StdAfx.h"
#include "skinnableframe.h"

SkinnableFrame::SkinnableFrame(void)
{
	type = FrameType;
	title = _T("SkinnableFrame");
	resizeable = 1;
	minwidth = 0;
	maxwidth = 100000;
	minheight = 0;
	maxheight = 100000;
}

SkinnableFrame::~SkinnableFrame(void)
{
}

int SkinnableFrame::Create(HWND parent)
{
	title = titlebar.title;
	if(SkinnableWindow::Create(parent)){
		if(resizeable){
			resize.Load("buttons::resize");
			resize.Create(hWnd);
		}
		titlebar.Create(hWnd);
		minimize.Load("buttons::minimize");
		close.Load("buttons::close");
		minimize.Create(titlebar.hWnd);
		minimize.SetMessageSink(hWnd);
		close.Create(titlebar.hWnd);
		close.SetMessageSink(hWnd);

		minimize.SetLocation(size.Width-minimize.fromright,minimize.location.Y);
		close.SetLocation(size.Width-close.fromright,close.location.Y);
		resize.SetLocation(size.Width-resize.fromright,size.Height-resize.frombottom);

		SetMinMaxSize(50,100000,titlebar.size.Height+resize.size.Height,100000);
		return 1;
	} else
		return 0;
}

void SkinnableFrame::SetSize(int width, int height)
{
	// limit the smallest and largets sizes this frame can shrink/grow to	
	if(height>maxheight)
		height = maxheight;
	
	if(height<minheight)
		height = minheight;

	if(width>maxwidth)
		width = maxwidth;

	if(width<minwidth)
		width = minwidth;

	SkinnableWindow::SetSize(width,height);
}

void SkinnableFrame::SetMinMaxSize(int minw, int maxw, int minh, int maxh)
{
	if(minw!=-1)
		minwidth = minw;
	if(maxw!=-1)
		maxwidth = maxw;
	if(minh!=-1)
		minheight = minh;
	if(maxh!=-1)
		maxheight = maxh;
	SetSize(size.Width,size.Height);
}

void SkinnableFrame::OnSize(int cx, int cy)
{
	SkinnableWindow::OnSize(cx,cy);

	// resize the title bar
	titlebar.SetSize(size.Width-1,titlebar.size.Height);

	//	move the frame's buttons
	minimize.SetLocation(size.Width-minimize.fromright,minimize.location.Y);
	close.SetLocation(size.Width-close.fromright,close.location.Y);
	resize.SetLocation(size.Width-resize.fromright,size.Height-resize.frombottom);

}

void SkinnableFrame::OnLoseFocus()
{
	titlebar.SetState(AppearanceState::normal);
	//print(L"%x loses focus\n",hWnd);
}

void SkinnableFrame::OnGainFocus()
{
	titlebar.SetState(AppearanceState::selected);
	//print(L"%x gains focus\n",hWnd);
}