// DlgServerSelect.cpp : implementation file
//

#include "stdafx.h"
#include "RYLLogin.h"
#include "DlgServerSelect.h"
#include "RYLLoginDlg.h"


// CDlgServerSelect dialog

IMPLEMENT_DYNAMIC(CDlgServerSelect, CDialog)
CDlgServerSelect::CDlgServerSelect(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgServerSelect::IDD, pParent)
{

}

CDlgServerSelect::~CDlgServerSelect()
{
}

void CDlgServerSelect::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_SERVER, m_ServerList);
}


BEGIN_MESSAGE_MAP(CDlgServerSelect, CDialog)
	ON_WM_PAINT()
	ON_WM_CTLCOLOR()
	ON_LBN_SELCHANGE(IDC_LIST_SERVER, OnLbnSelchangeListServer)
	ON_WM_LBUTTONDOWN()
	ON_LBN_DBLCLK(IDC_LIST_SERVER, OnLbnDblclkListServer)
END_MESSAGE_MAP()



void CDlgServerSelect::RenderBack( CDC &rDC, CBitmap &rBitmap, int Px, int Py , int SizeX, int SizeY )
{
	CBitmap *pOldBitmap;
	CDC memDC;
	memDC.CreateCompatibleDC(&rDC);
	pOldBitmap=memDC.SelectObject(&rBitmap);	
	CSize sBitmap=rBitmap.GetBitmapDimension();

	BITMAP bmInfo;
	rBitmap.GetBitmap(&bmInfo);	

	if( SizeX == -1 && SizeY == -1 )
		rDC.BitBlt(Px,Py,bmInfo.bmWidth,bmInfo.bmHeight,&memDC,0,0,SRCCOPY);
	else
		rDC.BitBlt(Px,Py,SizeX,SizeY,&memDC,0,0,SRCCOPY);

	memDC.SelectObject(pOldBitmap);

}

// CDlgServerSelect message handlers

void CDlgServerSelect::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	RenderBack( dc, m_btmBack, 0, 0 );

	// TODO: Add your message handler code here
	// Do not call CDialog::OnPaint() for painting messages
}

HBRUSH CDlgServerSelect::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	switch(nCtlColor)
	{
		case CTLCOLOR_LISTBOX:
			pDC->SetBkMode(TRANSPARENT);
			pDC->SetTextColor(RGB( 200,200,200 ));
			return (HBRUSH)GetStockObject(NULL_BRUSH);
		break;
	}

	// TODO:  Change any attributes of the DC here

	// TODO:  Return a different brush if the default is not desired
	return hbr;
}

void CDlgServerSelect::OnLbnSelchangeListServer()
{
//	m_ServerList.Invalidate( FALSE );
	Invalidate( FALSE );
	// TODO: Add your control notification handler code here
}

void CDlgServerSelect::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	if( point.x >= 173 && point.x <= 284 &&
		point.y >= 133 && point.y <= 170 )
	{
//		OnLbnDblclkListServer();
	}
	PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y) ); 

	CDialog::OnLButtonDown(nFlags, point);
}

void CDlgServerSelect::OnOK()
{
	// TODO: Add your specialized code here and/or call the base class

	//CDialog::OnOK();
}

void CDlgServerSelect::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	//CDialog::OnCancel();
}

void CDlgServerSelect::OnLbnDblclkListServer()
{
	// TODO: Add your control notification handler code here

	CRYLLoginDlg *pDlg = (CRYLLoginDlg*) AfxGetApp()->m_pMainWnd;

	if( m_ServerList.GetCurSel() == -1 )
	{
		
		AfxMessageBox( RYLLoginGetMessage( 0 ) );
		return;
	}
	ShowWindow( SW_HIDE );
	
	pDlg->ExecuteClient( m_ServerList.GetCurSel() );	
}
