// ConnectDlg.cpp : 구현 파일입니다.
//

#include "stdafx.h"
#include "ConsoleApp.h"
#include "ConnectDlg.h"

#include "ConnectProxyDlg.h"
#include ".\connectdlg.h"


// CConnectDlg 대화 상자입니다.
#define TIMER_ID_FOCUS 1

IMPLEMENT_DYNAMIC(CConnectDlg, CDialog)
CConnectDlg::CConnectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConnectDlg::IDD, pParent)
	, m_sUserId(_T(""))
	, m_sPassword(_T(""))
	, m_bAutoConnect(FALSE)
	, m_bSavePassword(FALSE)
{
}

CConnectDlg::~CConnectDlg()
{
}

void CConnectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STC_LOGO,         m_stcLogo);
	DDX_Text(pDX,    IDC_EDT_USERID,       m_sUserId);
	DDX_Text(pDX,    IDC_EDT_PASSWORD,     m_sPassword);
	DDX_Check(pDX,   IDC_CHK_AUTOCONNECT,  m_bAutoConnect);
	DDX_Check(pDX,   IDC_CHK_SAVEPASSWORD, m_bSavePassword);
	DDX_Control(pDX, IDC_CHK_AUTOCONNECT,  m_chkAutoConnect);
	DDX_Control(pDX, IDC_CHK_SAVEPASSWORD, m_chkSavePassword);
}


BEGIN_MESSAGE_MAP(CConnectDlg, CDialog)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_PROXY, OnBtnProxy)
	ON_BN_CLICKED(IDC_CHK_AUTOCONNECT, OnChkAutoconnect)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()


// CConnectDlg 메시지 처리기입니다.

BOOL CConnectDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

//	m_stcLogo.SetBitmap(::LoadBitmap(AfxGetInstanceHandle(), (LPCSTR)IDB_LOGO));

	CenterWindow();
	ShowWindow(SW_SHOW);

	m_nTimer = (UINT)SetTimer(TIMER_ID_FOCUS, 100, 0);
	CBrush brBkgnd;
	brBkgnd.CreateSysColorBrush(COLOR_BTNFACE);

	RECT rc;
	rc.left   = rc.top = 0;
	rc.right  = 147;
	rc.bottom = 61;

	CClientDC cdcSrc(this);
	CClientDC cdcDst(this);
	HDC hdcSrc = ::CreateCompatibleDC(cdcSrc.m_hDC);
	HDC hdcDst = ::CreateCompatibleDC(cdcDst.m_hDC);
	HBITMAP hbmSrc = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_LOGO));
	HBITMAP hbmDst = ::CreateCompatibleBitmap(cdcDst.m_hDC, rc.right, rc.bottom);
	HBITMAP hbmOldSrc = (HBITMAP)SelectObject(hdcSrc, hbmSrc);
	HBITMAP hbmOldDst = (HBITMAP)SelectObject(hdcDst, hbmDst);

	::FillRect(hdcDst, &rc, brBkgnd);

	TransparentBlt(hdcDst, 0, 0, 144, 36, hdcSrc, 0, 0, 144, 36, RGB(255, 255, 255));

	SelectObject(hdcSrc, hbmOldSrc);
	SelectObject(hdcDst, hbmOldDst);

	// Attach to Bitmap and Replace image in CImageList
	m_stcLogo.SetBitmap(hbmDst);

	DeleteDC(hdcSrc);
	DeleteDC(hdcDst);
	DeleteObject(hbmSrc);
//	DeleteObject(hbmDst);

	brBkgnd.DeleteObject();

	return TRUE;  // return TRUE unless you set the focus to a control
	// 예외: OCX 속성 페이지는 FALSE를 반환해야 합니다.
}

void CConnectDlg::OnTimer(UINT nIDEvent)
{
	KillTimer(m_nTimer);
	m_nTimer = 0;

	if (m_sUserId == "")
		GetDlgItem(IDC_EDT_USERID)->SetFocus();
	else if (m_sPassword == "")
		GetDlgItem(IDC_EDT_PASSWORD)->SetFocus();
	else
		GetDlgItem(IDOK)->SetFocus();

	CDialog::OnTimer(nIDEvent);
}

void CConnectDlg::OnOK()
{
	UpdateData(TRUE);

	CDialog::OnOK();
}

void CConnectDlg::OnChkAutoconnect()
{
	if (m_chkAutoConnect.GetCheck() == BST_CHECKED) {
		m_chkSavePassword.SetCheck(BST_CHECKED);
		m_chkSavePassword.EnableWindow(FALSE);
	}
	else {
		m_chkSavePassword.EnableWindow(TRUE);
	}
}

void CConnectDlg::OnBtnProxy()
{
	CConnectProxyDlg dlg;

	dlg.DoModal();
}

void CConnectDlg::OnBnClickedOk()
{
	// TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
	OnOK();
}
