#include "StdAfx.h"
#include "loginwindow.h"

#define STATUS(x) status.SetText(env.GetWideString("login::status",x,_T(x)))

LoginWindow::LoginWindow(void)
{
}

LoginWindow::~LoginWindow(void)
{
}

void LoginWindow::OnPaint(HWND hWnd, HDC hdc)
{
	SkinnableWindow::OnPaint(hWnd,hdc);

}

int LoginWindow::Create(HWND parent)
{
	Load("login");
	title = env.GetWideString("login","title",L"LoginWindow");
	if(StaticWindow::Create(parent)){
		// create and place the ok and cancel buttons
		ok.Load("login::ok");
		ok.Create(hWnd);
		cancel.Load("login::cancel");
		cancel.Create(hWnd);

		// create and place the textboxes
		username.Load("login::username");
		username.Create(hWnd);
		password.Load("login::password");
		password.Create(hWnd,ES_LEFT|ES_PASSWORD);
		status.Load("login::status");
		status.Create(hWnd,ES_LEFT|ES_READONLY);
		status.Disable();
		STATUS("DefaultStatus");

		username.SetTabTo(password.hEditCtrl);
		password.SetTabTo(username.hEditCtrl);
		// this will cause an enter in the username or password fields to look like a click on the ok button
		username.SetMsgOnCarriageReturn(hWnd,WM_COMMAND,MAKEWPARAM(0,ID_LEFTBUTTON),(LPARAM)ok.hWnd);
		password.SetMsgOnCarriageReturn(hWnd,WM_COMMAND,MAKEWPARAM(0,ID_LEFTBUTTON),(LPARAM)ok.hWnd);
		return 1;
	} else
		return 0;
}

void LoginWindow::OnButton(HWND h)
{
	if(h==ok.hWnd){
		print(L"LoginWindow: ok pressed\n");
		STATUS("LoggingOn");
		theApp.LogOn(username.Data(),password.Data());
		password.SetText(L"");
	} else if(h==cancel.hWnd){
		print(L"LoginWindow: cancel pressed\n");
		Hide();
		password.SetText(L"");
	} else {
		print(L"LoginWindow: unknown button pressed\n");
	}
}
