// DlgSettings.cpp : 实现文件
//

#include "stdafx.h"
#include "Draw.h"
#include "2DObject.h"
#include "DlgSettings.h"


// CDlgSettings 对话框

IMPLEMENT_DYNAMIC(CDlgSettings, CDialog)

CDlgSettings::CDlgSettings(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSettings::IDD, pParent)
	, m_nTypeObj(0)
	, m_R(0)
	, m_G(0)
	, m_B(0)
	, m_bHide(TRUE)
{
	for (int i=0; i!=6; ++i)
	{
		m_byR[i] = g_2DObj.m_byR[i];
		m_byG[i] = g_2DObj.m_byG[i];
		m_byB[i] = g_2DObj.m_byB[i];
		if (i != 5)
		{
			m_arLineWidth[i] = g_2DObj.m_wLineWidth[i];
			m_arLineStyle[i] = g_2DObj.m_byLineStyle[i];
		}
	}

	m_bHide = g_2DObj.m_bHide;
}

CDlgSettings::~CDlgSettings()
{
}

void CDlgSettings::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Radio(pDX, IDC_RADIO1, m_nTypeObj);
	DDX_Control(pDX, IDC_STATIC_COLOR, m_wndClr);
	DDX_Text(pDX, IDC_EDIT_R, m_R);
	DDX_Text(pDX, IDC_EDIT_G, m_G);
	DDX_Text(pDX, IDC_EDIT_B, m_B);
	DDX_Check(pDX, IDC_CHECK1, m_bHide);
	DDX_Control(pDX, IDC_COMBO_LINESTYLE, m_cbLineStyle);
	DDX_Control(pDX, IDC_SPIN_LINEWIDTH, m_spLine);
	DDX_Control(pDX, IDC_EDIT_LINEWIDTH, m_edLineWidth);
	DDX_Control(pDX, IDC_COMBO_CURVE, m_cbCurveStyle);
}


BEGIN_MESSAGE_MAP(CDlgSettings, CDialog)
	ON_BN_CLICKED(IDC_RADIO1, &CDlgSettings::OnBnClickedRadio1)
	ON_BN_CLICKED(IDC_RADIO2, &CDlgSettings::OnBnClickedRadio2)
	ON_BN_CLICKED(IDC_RADIO3, &CDlgSettings::OnBnClickedRadio3)
	ON_BN_CLICKED(IDC_RADIO4, &CDlgSettings::OnBnClickedRadio4)
	ON_BN_CLICKED(IDC_RADIO5, &CDlgSettings::OnBnClickedRadio5)
	ON_BN_CLICKED(IDC_RADIO6, &CDlgSettings::OnBnClickedRadio6)
//	ON_WM_PAINT()
ON_WM_CTLCOLOR()
ON_STN_CLICKED(IDC_STATIC_COLOR, &CDlgSettings::OnStnClickedStaticColor)
ON_BN_CLICKED(IDOK, &CDlgSettings::OnBnClickedOk)
//ON_EN_UPDATE(IDC_EDIT_R, &CDlgSettings::OnEnUpdateEditR)
ON_EN_CHANGE(IDC_EDIT_R, &CDlgSettings::OnEnChangeEditR)
ON_EN_CHANGE(IDC_EDIT_G, &CDlgSettings::OnEnChangeEditG)
ON_EN_CHANGE(IDC_EDIT_B, &CDlgSettings::OnEnChangeEditB)
ON_CBN_SELCHANGE(IDC_COMBO_LINESTYLE, &CDlgSettings::OnCbnSelchangeComboLinestyle)
ON_CBN_SELCHANGE(IDC_COMBO_CURVE, &CDlgSettings::OnCbnSelchangeComboCurve)
ON_EN_CHANGE(IDC_EDIT_LINEWIDTH, &CDlgSettings::OnEnChangeEditLinewidth)
END_MESSAGE_MAP()


// CDlgSettings 消息处理程序

void CDlgSettings::OnBnClickedRadio1()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_nTypeObj == 0)
	{
		ShowHideWidget(m_nTypeObj);
	}
}

void CDlgSettings::ShowHideWidget( int nType )
{
	switch (nType)
	{
	case 0:
		{
			GetDlgItem(IDC_STATIC_LINESTYLE)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_STATIC_LINEWIDTH)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_STATIC_CURVE)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_COMBO_LINESTYLE)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_EDIT_LINEWIDTH)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_SPIN_LINEWIDTH)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_COMBO_CURVE)->ShowWindow(SW_HIDE);
		}
		break;
	case 1:
	case 2:
	case 4:
	case 5:
		{
			GetDlgItem(IDC_STATIC_LINESTYLE)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_STATIC_LINEWIDTH)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_STATIC_CURVE)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_COMBO_LINESTYLE)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_EDIT_LINEWIDTH)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_SPIN_LINEWIDTH)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_COMBO_CURVE)->ShowWindow(SW_HIDE);
		}
		break;
	case 3:
		{
			GetDlgItem(IDC_STATIC_LINESTYLE)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_STATIC_LINEWIDTH)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_STATIC_CURVE)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_COMBO_LINESTYLE)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_EDIT_LINEWIDTH)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_SPIN_LINEWIDTH)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_COMBO_CURVE)->ShowWindow(SW_SHOW);
		}
		break;
	default:
		;
	}
	if(nType != 0)
	{
		m_cbLineStyle.SetCurSel(m_arLineStyle[nType-1]);
		m_spLine.SetPos(m_arLineWidth[nType-1]);
	}
	m_wndClr.Invalidate(FALSE);
}

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

	// TODO:  在此添加额外的初始化
    ShowHideWidget(m_nTypeObj);

	static TCHAR* szLineStyles[] = {
		_T("实线"),
		_T("虚线"),
	    _T("点线"),
	    _T("点划线"),
	    _T("双点划线"),
	    NULL};
	static TCHAR* szCurveStyles[] = {
	    _T("贝赛尔曲线"),
		NULL
		};
    int i = 0;
	m_cbLineStyle.ResetContent();
	while(szLineStyles[i] != NULL)
	{
		m_cbLineStyle.AddString(szLineStyles[i]);
		++i;
	}
	m_cbLineStyle.SetCurSel(0);
	i = 0;
	while(szCurveStyles[i] != NULL)
	{
		m_cbCurveStyle.AddString(szCurveStyles[i]);
        ++i;
	}
	m_cbCurveStyle.SetCurSel(0);


	m_spLine.SetBuddy(&m_edLineWidth);
	m_spLine.SetRange(1,10);
	m_spLine.SetPos(1);


    UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void CDlgSettings::OnBnClickedRadio2()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_nTypeObj == 1)
	{
		ShowHideWidget(m_nTypeObj);
	}
}

void CDlgSettings::OnBnClickedRadio3()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_nTypeObj == 2)
	{
		ShowHideWidget(m_nTypeObj);
	}
}

void CDlgSettings::OnBnClickedRadio4()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_nTypeObj == 3)
	{
		ShowHideWidget(m_nTypeObj);
	}
}

void CDlgSettings::OnBnClickedRadio5()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_nTypeObj == 4)
	{
		ShowHideWidget(m_nTypeObj);
	}
}

void CDlgSettings::OnBnClickedRadio6()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_nTypeObj == 5)
	{
		ShowHideWidget(m_nTypeObj);
	}
}

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

	// TODO:  在此更改 DC 的任何属性

	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	if (pWnd)
	{
		if (pWnd==(CWnd*)&m_wndClr)
		{
			COLORREF clr = RGB(255,255,255);
			switch(m_nTypeObj)
			{
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
				{
					m_R = m_byR[m_nTypeObj-1];
					m_G = m_byG[m_nTypeObj-1];
					m_B = m_byB[m_nTypeObj-1];
					clr = GetClrRefFromByte(m_byR[m_nTypeObj-1],m_byG[m_nTypeObj-1],m_byB[m_nTypeObj-1]);
				}
				break;
			case 0:
				{
					m_R = m_byR[5];
					m_G = m_byG[5];
					m_B = m_byB[5];
					clr = GetClrRefFromByte(m_byR[5],m_byG[5],m_byB[5]);
				}
			default:
				;
			}
			pDC->SetBkColor(clr);
			UpdateData(FALSE);
			return CreateSolidBrush(clr);
		}
	}
	return hbr;
}

void CDlgSettings::OnStnClickedStaticColor()
{
	// TODO: 在此添加控件通知处理程序代码
    COLORREF clr = m_wndClr.GetDC()->GetBkColor();
	CColorDialog dlg(clr);
	if (dlg.DoModal()==IDOK)
	{
		clr = dlg.GetColor();
		switch(m_nTypeObj)
		{
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			{
				GetRGBFromClrRef(clr,&m_byR[m_nTypeObj-1],&m_byG[m_nTypeObj-1],&m_byB[m_nTypeObj-1]);
			}
			break;
		case 0:
			{
				GetRGBFromClrRef(clr,&m_byR[5],&m_byG[5],&m_byB[5]);
			}
		default:
			;
		}
		m_wndClr.Invalidate(FALSE);
	}
}

void CDlgSettings::OnEnChangeEditR()
{
	// TODO:  如果该控件是 RICHEDIT 控件，则它将不会
	// 发送该通知，除非重写 CDialog::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	UpdateData(TRUE);
	switch(m_nTypeObj)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		{
			m_byR[m_nTypeObj-1] = m_R; 
		}
		break;
	case 0:
		{
			m_byR[5] = m_R;
		}
	default:
		;
	}
	m_wndClr.Invalidate(FALSE);
}

void CDlgSettings::OnEnChangeEditG()
{
	// TODO:  如果该控件是 RICHEDIT 控件，则它将不会
	// 发送该通知，除非重写 CDialog::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	UpdateData(TRUE);
	switch(m_nTypeObj)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		{
			m_byG[m_nTypeObj-1] = m_G; 
		}
		break;
	case 0:
		{
			m_byG[5] = m_G;
		}
	default:
		;
	}
	m_wndClr.Invalidate(FALSE);
}

void CDlgSettings::OnEnChangeEditB()
{
	// TODO:  如果该控件是 RICHEDIT 控件，则它将不会
	// 发送该通知，除非重写 CDialog::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	UpdateData(TRUE);
	switch(m_nTypeObj)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		{
			m_byB[m_nTypeObj-1] = m_B; 
		}
		break;
	case 0:
		{
			m_byB[5] = m_B;
		}
	default:
		;
	}
	m_wndClr.Invalidate(FALSE);
}

void CDlgSettings::OnCbnSelchangeComboLinestyle()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_nTypeObj != 0)
	{
		int nSel = m_cbLineStyle.GetCurSel();
		m_arLineStyle[m_nTypeObj-1] = nSel;
	}
}

void CDlgSettings::OnCbnSelchangeComboCurve()
{
	// TODO: 在此添加控件通知处理程序代码
}

void CDlgSettings::OnEnChangeEditLinewidth()
{
	// TODO:  如果该控件是 RICHEDIT 控件，则它将不会
	// 发送该通知，除非重写 CDialog::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	if (m_nTypeObj != 0)
	{
		m_arLineWidth[m_nTypeObj-1] = m_spLine.GetPos();
	}	
}

void CDlgSettings::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	for (int i=0; i!=6; ++i)
	{
		g_2DObj.m_byR[i] = m_byR[i];
		g_2DObj.m_byG[i] = m_byG[i];
		g_2DObj.m_byB[i] = m_byB[i];
		if (i != 5)
		{
			g_2DObj.m_wLineWidth[i] = m_arLineWidth[i];
			g_2DObj.m_byLineStyle[i] = m_arLineStyle[i];
		}
	}
	g_2DObj.m_bHide = m_bHide;
	OnOK();
}