#include "Stdinc.h"
#include "cDisplay.h"

#include "cTearsAppWindow.h"
#include "cTearsApp.h"

#include "resource.h"
#include "TOOLMemory.h"

BOOL cTearsAppWindow::ClassRegistered = false;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cTearsAppWindow::cTearsAppWindow(){
	pMainChild = NULL;
}

cTearsAppWindow::~cTearsAppWindow(){
	Destroy();
}

BOOL cTearsAppWindow::RegisterClass(){

	ClassRegistered = cWindow::RegisterClass(
		CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW,
		LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEARS)),
		LoadCursor(hInstance, MAKEINTRESOURCE(IDC_TEARS)),
		NULL);
	return ClassRegistered;
}

BOOL cTearsAppWindow::Create(){
	DWORD Style = WS_VISIBLE | WS_SYSMENU | WS_POPUP | WS_CLIPCHILDREN;
	DWORD StyleEx = WS_EX_APPWINDOW;
	RECT rcClient = {0, 0, 640, 480};

	AdjustWindowRectEx(&rcClient, Style, FALSE, StyleEx);
	return cWindow::Create(
		StyleEx, // Extended Window Style
		"티어즈", // Caption
		Style, // Window Style
		CW_USEDEFAULT, CW_USEDEFAULT, // x, y
		rcClient.right - rcClient.left, 
		rcClient.bottom - rcClient.top, // Width, Height
		NULL, // Parent Window
		NULL); // Menu
}

//////////////////////////////////////////////////////////////////////
// Window Prodedure
//////////////////////////////////////////////////////////////////////

LRESULT cTearsAppWindow::Proc(UINT uMsg, WPARAM wParam, LPARAM lParam){
	switch(uMsg){
	case WM_CLOSE:
		if(OnClose()) return 0;
		break;
	case WM_DISPLAYCHANGE:
		OnDisplayChange(wParam, lParam);
		break;
	case WM_SETFOCUS:
		if(pMainChild) pMainChild->SetFocus();
		break;
	}
	return DefProc(uMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////
// Display Change
//////////////////////////////////////////////////////////////////////

void cTearsAppWindow::OnDisplayChange(WPARAM wParam, LPARAM lParam){
	WINDOWPLACEMENT wndpl;
	RECT rcWindow;
	RECT rcClient = {0, 0, 640, 480};

	// 필요한 프레임 윈도우의 크기를 계산한다.
	AdjustWindowRectEx(&rcClient, 
		GetWindowLong(hWnd, GWL_STYLE),
		FALSE,
		GetWindowLong(hWnd, GWL_EXSTYLE));

	// 윈도우의 본래 위치를 파악한다.
	wndpl.length = sizeof(WINDOWPLACEMENT);
	GetWindowPlacement(hWnd, &wndpl);
	rcWindow = wndpl.rcNormalPosition;

	wndpl.flags = 0; 
	wndpl.showCmd = SW_SHOWNA; 
    wndpl.ptMaxPosition.x = 0; 
    wndpl.ptMaxPosition.y = 0; 
	SetRect(&wndpl.rcNormalPosition,
		rcWindow.left, rcWindow.top,
		rcWindow.left + rcClient.right - rcClient.left,
		rcWindow.top + rcClient.bottom - rcClient.top);

	SetWindowPlacement(hWnd, &wndpl);
}

//////////////////////////////////////////////////////////////////////
// 인터페이스 초기화 작업
//////////////////////////////////////////////////////////////////////

BOOL cTearsAppWindow::OnClose(){
// 프로그램을 정말 끝낼 것인지를 물어보는 문자열을 출력한다.
//	if(MessageBox(hWnd, "정말로 종료 하시겠습니까?", "확인", MB_YESNO) == IDYES){
		PostQuitMessage(MLEC_EXITAPP);
//		return true;
//	}
	return true;
}

