#include "StdAfx.h"
#include "moveablebutton.h"

MoveableButton::MoveableButton(void)
{
	constrain = 0;
}

MoveableButton::~MoveableButton(void)
{
}

void MoveableButton::SetMoveableRect(int x, int y, int w, int h)
{
	move = Rect(x,y,w,h);
	constrain = 1;
}

void MoveableButton::SetLocation(int x, int y)
{
	if(constrain){
		// if the new position is outside the moveable rect than project it onto the rectangle
		if(x<move.X)
			x = move.X;
		if(y<move.Y)
			y = move.Y;
		if(x>=move.GetRight())
			x = move.GetRight()-1;
		if(y>=move.GetBottom())
			y = move.GetBottom()-1;
	}
	
	SkinnableButton::SetLocation(x,y);
}

void MoveableButton::OnMouseMove()
{
	// Moving the window
	if(env.mouse.LeftButton){
		int x,y;
		x = env.mouse.InitialPos.x+(env.mouse.Screen.x-env.mouse.DragOrigin.x);
		y = env.mouse.InitialPos.y+(env.mouse.Screen.y-env.mouse.DragOrigin.y);

		SetLocation(x,y);

		//notify parent of movement
		if(hWndSink)
			SafeSendMessage(hWndSink,WM_COMMAND,MAKEWPARAM(0,ID_BUTTONMOVE),(LPARAM)hWnd);
		if(hWndParent)
			SafeSendMessage(hWndParent,WM_COMMAND,MAKEWPARAM(0,ID_BUTTONMOVE),(LPARAM)hWnd);
	} else  {
		if(GetFocus()==hWnd){
			
		} else {
			SetState(AppearanceState::mouseover);
		}
	}
}