// ★コメント未記入
// PlugInKeyAssignDlg.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "shitarabian.h"
#include "PlugInKeyAssignDlg.h"
#include "Common.h"
#include "PlugInKeyEditDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPlugInKeyAssignDlg ダイアログ


CPlugInKeyAssignDlg::CPlugInKeyAssignDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlugInKeyAssignDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlugInKeyAssignDlg)
	m_bEnable = FALSE;
	//}}AFX_DATA_INIT
}


void CPlugInKeyAssignDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlugInKeyAssignDlg)
	DDX_Control(pDX, IDC_KEY_ADD, m_btnAdd);
	DDX_Control(pDX, IDC_KEY_DELETE, m_btnDelete);
	DDX_Control(pDX, IDC_KEY_EDIT, m_btnEdit);
	DDX_Control(pDX, IDC_KEY_LIST, m_listKeyz);
	DDX_Check(pDX, IDC_KEY_ENABLE, m_bEnable);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPlugInKeyAssignDlg, CDialog)
	//{{AFX_MSG_MAP(CPlugInKeyAssignDlg)
	ON_NOTIFY(NM_CLICK, IDC_KEY_LIST, OnClickKeyList)
	ON_BN_CLICKED(IDC_KEY_EDIT, OnKeyEdit)
	ON_BN_CLICKED(IDC_KEY_ADD, OnKeyAdd)
	ON_NOTIFY(NM_RCLICK, IDC_KEY_LIST, OnClickKeyList)
	ON_BN_CLICKED(IDC_KEY_DELETE, OnKeyDelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlugInKeyAssignDlg メッセージ ハンドラ

BOOL CPlugInKeyAssignDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	
	m_listKeyz.InsertColumn(	0,	"名称"	);
	m_listKeyz.InsertColumn(	1,	"キー"	);
	m_listKeyz.InsertColumn(	2,	"Shift"	);
	m_listKeyz.InsertColumn(	3,	"Ctrl"	);
	m_listKeyz.InsertColumn(	4,	"Alt"	);
	
	m_listKeyz.SetColumnWidth(	0,	120		);
	m_listKeyz.SetColumnWidth(	1,	100		);
	m_listKeyz.SetColumnWidth(	2,	50		);
	m_listKeyz.SetColumnWidth(	3,	50		);
	m_listKeyz.SetColumnWidth(	4,	50		);

	RefreshItems();
	return TRUE;  // コントロールにフォーカスを設定しないとき、戻り値は TRUE となります
	              // 例外: OCX プロパティ ページの戻り値は FALSE となります
}

void CPlugInKeyAssignDlg::OnClickKeyList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// warning C4100:警告の無視
	pNMHDR;

	VisibleControl();
	*pResult = 0;
}

void CPlugInKeyAssignDlg::VisibleControl()
{
	int		idxSelect = m_listKeyz.GetSelectionMark();
	BOOL	bSelect = ( idxSelect != -1 );

	if ( bSelect )
	{
		m_btnEdit.EnableWindow( bSelect );
		m_btnDelete.EnableWindow( !m_aKeyDefinez[idxSelect].bSystemDefine );
	}
	else
	{
		m_btnEdit.EnableWindow( FALSE );
		m_btnDelete.EnableWindow( FALSE );
	}
}

void CPlugInKeyAssignDlg::OnKeyEdit() 
{
	int	idxKey = m_listKeyz.GetSelectionMark();

	CPlugInKeyEditDlg	dlg;
	dlg.m_tKeyDefine = m_aKeyDefinez[idxKey];
	if ( dlg.DoModal() == IDOK )
	{
		m_aKeyDefinez[idxKey] = dlg.m_tKeyDefine;
		m_listKeyz.SetItemText( idxKey, 0, m_aKeyDefinez[idxKey].strKeyFunctionName );
		m_listKeyz.SetItemText( idxKey, 1, ::VKToNickname( m_aKeyDefinez[idxKey].iDstKey ) );
		m_listKeyz.SetItemText( idxKey, 2, ::BoolToMaruBatu( m_aKeyDefinez[idxKey].bDstShift ));
		m_listKeyz.SetItemText( idxKey, 3, ::BoolToMaruBatu( m_aKeyDefinez[idxKey].bDstCtrl ));
		m_listKeyz.SetItemText( idxKey, 4, ::BoolToMaruBatu( m_aKeyDefinez[idxKey].bDstAlt ));
	}
}
void CPlugInKeyAssignDlg::RefreshItems() 
{
	m_listKeyz.DeleteAllItems();
	int	idxKey;
	for( idxKey=0 ; idxKey<m_aKeyDefinez.GetSize() ; idxKey++ )
	{
		m_listKeyz.InsertItem(	idxKey,	m_aKeyDefinez[idxKey].strKeyFunctionName );
		m_listKeyz.SetItemText( idxKey, 1, ::VKToNickname( m_aKeyDefinez[idxKey].iDstKey ) );
		m_listKeyz.SetItemText( idxKey, 2, ::BoolToMaruBatu( m_aKeyDefinez[idxKey].bDstShift ));
		m_listKeyz.SetItemText( idxKey, 3, ::BoolToMaruBatu( m_aKeyDefinez[idxKey].bDstCtrl ));
		m_listKeyz.SetItemText( idxKey, 4, ::BoolToMaruBatu( m_aKeyDefinez[idxKey].bDstAlt ));
	}

	VisibleControl();
}

void CPlugInKeyAssignDlg::OnKeyAdd() 
{
	TKeyDefine	tKeyDefine;
	tKeyDefine.bSystemDefine = FALSE;
	//tKeyDefine.strKeyFunctionName;
	tKeyDefine.iSrcKey = ' ';
	tKeyDefine.bSrcShift = FALSE;
	tKeyDefine.bSrcCtrl = FALSE;
	tKeyDefine.bSrcAlt = FALSE;
	tKeyDefine.iDstKey = ' ';
	tKeyDefine.bDstShift = FALSE;
	tKeyDefine.bDstCtrl = FALSE;
	tKeyDefine.bDstAlt = FALSE;

	CPlugInKeyEditDlg	dlg;
	dlg.m_tKeyDefine = tKeyDefine;
	if ( dlg.DoModal() == IDOK )
	{
		m_aKeyDefinez.Add( dlg.m_tKeyDefine );
		RefreshItems();
	}	
}

void CPlugInKeyAssignDlg::OnKeyDelete() 
{
	int	idxKey = m_listKeyz.GetSelectionMark();

	CPlugInKeyEditDlg	dlg;
	m_aKeyDefinez.RemoveAt(idxKey);
	RefreshItems();
}
