// CategoryInfoEditorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CategoryInfoEditorDlg.h"
#include "XmlGuiCategoryInfo.h"
#include "XmlGuiCategoryInfoList.h"
#include "GuiDefs.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCategoryInfoEditorDlg dialog


CCategoryInfoEditorDlg::CCategoryInfoEditorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCategoryInfoEditorDlg::IDD, pParent),
	categoryInfo_(NULL),
	categoryList_(NULL),
	initialized_(false)
{
	//{{AFX_DATA_INIT(CCategoryInfoEditorDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CCategoryInfoEditorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCategoryInfoEditorDlg)
	DDX_Control(pDX, IDOK, cmdOk_);
	DDX_Control(pDX, IDCANCEL, cmdCancel_);
	DDX_Control(pDX, IDC_LBL_DESCRIPTION, lblDescription_);
	DDX_Control(pDX, IDC_LBL_CAPTION, lblCaption_);
	DDX_Control(pDX, IDC_CHK_SORT, chkSort_);
	DDX_Control(pDX, IDC_TXT_DESCRIPTION, txtDescription_);
	DDX_Control(pDX, IDC_TXT_CAPTION, txtCaption_);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCategoryInfoEditorDlg, CDialog)
	//{{AFX_MSG_MAP(CCategoryInfoEditorDlg)
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCategoryInfoEditorDlg message handlers

BOOL CCategoryInfoEditorDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	txtCaption_.SetWindowText(categoryInfo_->GetCaption());
	txtDescription_.SetWindowText(categoryInfo_->GetDescription());
	chkSort_.SetCheck(categoryInfo_->GetSorted());

	initialized_ = true;

	UpdateWindowSize();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCategoryInfoEditorDlg::Init(CXmlGuiCategoryInfo* categoryInfo, CXmlGuiCategoryInfoList* categoryList)
{
	categoryInfo_ = categoryInfo;
	categoryList_ = categoryList;

}

void CCategoryInfoEditorDlg::OnOK()
{
	cmString tmp;
	CXmlGuiCategoryInfo* info;

	txtCaption_.GetWindowText(tmp);
	if (tmp == _T("")) {
		MessageBox(_T("Invalid caption"));
		return;
	}
	
	info = categoryList_->GetCategoryByCaption(tmp);
	if (info != categoryInfo_ && info != NULL) {
		tmp = _T("Caption \"") + tmp + _T("\" already in use");
		MessageBox(tmp);
		return;
	}
	
	categoryInfo_->SetCaption(tmp);
	categoryInfo_->SetSorted(chkSort_.GetCheck() == TRUE);
	txtDescription_.GetWindowText(tmp);
	categoryInfo_->SetDescription(tmp);
		
	CDialog::OnOK();
}

void CCategoryInfoEditorDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	UpdateWindowSize();	
}

void CCategoryInfoEditorDlg::UpdateWindowSize()
{
	if (!initialized_)
		return;

	CRect rc;
	CRect lblRect;
	CRect txtRect;
	CRect chkRect;
	CRect btnRect;
	int lblWidth;
	int txtWidth;
	int txtHeight;
	int top;

	GetClientRect(rc);

	chkSort_.GetWindowRect(chkRect);
	cmdCancel_.GetWindowRect(btnRect);

	lblCaption_.SetWindowPos(&CWnd::wndTop, BORDER, BORDER, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
	lblCaption_.GetWindowRect(lblRect);
	ScreenToClient(lblRect);
	
	lblWidth = lblRect.Width();
	txtWidth = rc.Width() - 3 * BORDER - lblWidth;
	txtCaption_.GetWindowRect(txtRect);
 	txtCaption_.SetWindowPos(&CWnd::wndTop, lblRect.right + BORDER, BORDER, txtWidth, txtRect.Height(), SWP_SHOWWINDOW);
	
	top = lblRect.bottom + BORDER;
	lblDescription_.SetWindowPos(&CWnd::wndTop, BORDER, top, lblWidth, lblRect.Height(), SWP_SHOWWINDOW);
	
	txtHeight = rc.Height() - top - 3 * BORDER - btnRect.Height() - chkRect.Height();
	txtDescription_.SetWindowPos(&CWnd::wndTop, lblRect.right + BORDER, top, txtWidth, txtHeight, SWP_SHOWWINDOW);
	txtDescription_.GetWindowRect(txtRect);
	ScreenToClient(txtRect);

	chkSort_.SetWindowPos(&CWnd::wndTop, txtRect.left, txtRect.bottom + BORDER, txtRect.Width(), chkRect.Height(), SWP_SHOWWINDOW);
	
	cmdCancel_.SetWindowPos(&CWnd::wndTop, rc.right - BORDER - btnRect.Width(), rc.bottom - BORDER - btnRect.Height(), 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
	cmdOk_.SetWindowPos(&CWnd::wndTop, rc.right - 2 * BORDER - 2 * btnRect.Width(), rc.bottom - BORDER - btnRect.Height(), 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);

	cmdCancel_.RedrawWindow();
	cmdOk_.RedrawWindow();
}

void CCategoryInfoEditorDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
	lpMMI->ptMinTrackSize.x = 350;
	lpMMI->ptMinTrackSize.y = 200;

	CDialog::OnGetMinMaxInfo(lpMMI);
}
