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

#include "stdafx.h"
#include "ConsoleApp.h"
#include "ConfigDlg.h"


// CConfigDlg 대화 상자입니다.

IMPLEMENT_DYNAMIC(CConfigDlg, CDialog)
CConfigDlg::CConfigDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConfigDlg::IDD, pParent)
{
	m_iPage = -1;
}

CConfigDlg::~CConfigDlg()
{
	for (int i = 0; i < m_paPage.GetCount(); i++)
		delete (PCONFIG_PAGE_STRUCT)m_paPage[i];
}

void CConfigDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STC_TITLE, m_stcTitle);
	DDX_Control(pDX, IDC_TAB, m_tab);
}


BEGIN_MESSAGE_MAP(CConfigDlg, CDialog)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB, OnSelchangeTb)
	ON_BN_CLICKED(IDOK, OnOk)
END_MESSAGE_MAP()


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

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

	m_stcTitle.m_crText      = ::GetSysColor(COLOR_3DFACE);
	m_stcTitle.m_nFontWeight = FW_BOLD;
	m_stcTitle.m_nFontSize   = 14;
	m_stcTitle.m_sFontName   = _T("Verdana");
	m_stcTitle.SetConstantText(APP_TITLE);

	m_tab.InsertItem(0, "기본");
	m_tab.InsertItem(1, "URL 저장");
	m_tab.InsertItem(2, "프록시");
//	m_tab.SetItemSize(CSize(130, 17));

	m_tab.GetClientRect(&m_rcFrame);
	m_rcFrame.left   += 1;
	m_rcFrame.top    += 23;
	m_rcFrame.right  -= 1;
	m_rcFrame.bottom -= 1;

	m_dlgCommon.m_bAutoConnect  = COptions::GetOptionVal(OPTION_AUTOCONNECT);
	m_dlgCommon.m_bSavePassword = COptions::GetOptionVal(OPTION_SAVEPASSWORD);
	m_dlgCommon.m_nAfterExec    = COptions::GetOptionVal(OPTION_AFTEREXEC);
	m_dlgUrl.m_bSearchSub       = COptions::GetOptionVal(OPTION_SEARCHSUBURL);
	m_dlgUrl.m_nTag             = COptions::GetOptionVal(OPTION_URLTAGTYPE);
	m_dlgUrl.m_sSTag            = COptions::GetOption(OPTION_URLSTAG);
	m_dlgUrl.m_sETag            = COptions::GetOption(OPTION_URLETAG);
	m_dlgProxy.m_sHost          = COptions::GetOption(OPTION_PROXYHOST);
	m_dlgProxy.m_sPort          = COptions::GetOption(OPTION_PROXYPORT);
	m_dlgProxy.m_bUseIEProxy    = COptions::GetOptionVal(OPTION_USEIEPROXY);

	AddPage(m_dlgCommon, "기본");
	AddPage(m_dlgUrl,    "URL 저장");
	AddPage(m_dlgProxy,  "프록시");

	ShowPage(0);

#ifdef _OPENDISK
	SetWindowText("Opensidk Console 설정");
#else
	SetWindowText("SP-Console 설정");
#endif

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

void CConfigDlg::AddPage(CConfigSubDlg &dlg, LPCTSTR pszCaption)
{
	PCONFIG_PAGE_STRUCT pcp = new CONFIG_PAGE_STRUCT;
	pcp->pdlg       = &dlg;
	pcp->nID        = dlg.GetID();
	pcp->sCaption   = pszCaption;

	m_paPage.Add(pcp);

	if (!::IsWindow(dlg.m_hWnd))
		dlg.Create(dlg.GetID(), this);
}

BOOL CConfigDlg::ShowPage(int iPage)
{
	if (m_iPage >= 0) {
		PCONFIG_PAGE_STRUCT pcp = (PCONFIG_PAGE_STRUCT)m_paPage[m_iPage];
		if (!pcp)
			return FALSE;

		if (pcp->pdlg) {
			if (::IsWindow(pcp->pdlg->GetSafeHwnd()))
				pcp->pdlg->ShowWindow(SW_HIDE);
		}
	}

	PCONFIG_PAGE_STRUCT pcp = (PCONFIG_PAGE_STRUCT)m_paPage[iPage];
	if (!pcp)
		return FALSE;

	if (pcp->pdlg) {
		if (::IsWindow(pcp->pdlg->GetSafeHwnd())) {
			pcp->pdlg->SetParent(&m_tab);
			pcp->pdlg->MoveWindow(m_rcFrame.left, m_rcFrame.top, m_rcFrame.Width(), m_rcFrame.Height());
			pcp->pdlg->ShowWindow(SW_SHOW);
			pcp->pdlg->RedrawWindow();
			pcp->pdlg->SetFocus();
		}

		m_tab.SetCurSel(iPage);

		m_iPage = iPage;
	}

	m_stcTitle.SetWindowText(pcp->sCaption);

	return TRUE;
}

void CConfigDlg::OnSelchangeTb(NMHDR *pNMHDR, LRESULT *pResult)
{
	ShowPage(m_tab.GetCurSel());

	*pResult = 0;
}

void CConfigDlg::OnOk()
{
	m_dlgCommon.UpdateData();
	m_dlgUrl.UpdateData();
	m_dlgProxy.UpdateData();

	COptions::SetOption(OPTION_AUTOCONNECT,  m_dlgCommon.m_bAutoConnect);
	COptions::SetOption(OPTION_SAVEPASSWORD, m_dlgCommon.m_bSavePassword);
	COptions::SetOption(OPTION_AFTEREXEC,    m_dlgCommon.m_nAfterExec);
	COptions::SetOption(OPTION_SEARCHSUBURL, m_dlgUrl.m_bSearchSub);
	COptions::SetOption(OPTION_URLTAGTYPE,   m_dlgUrl.m_nTag);
	COptions::SetOption(OPTION_URLSTAG,      m_dlgUrl.m_sSTag);
	COptions::SetOption(OPTION_URLETAG,      m_dlgUrl.m_sETag);
	COptions::SetOption(OPTION_PROXYHOST,    m_dlgProxy.m_sHost);
	COptions::SetOption(OPTION_PROXYPORT,    m_dlgProxy.m_sPort);
	COptions::SetOption(OPTION_USEIEPROXY,   m_dlgProxy.m_bUseIEProxy);

	CDialog::OnOK();
}
