// ★コメント未記入
/*-----------------------------------------------------------------------------
[名称]	ShitarabianPlugInKeyAssign.cpp
[概要]	キー割り当ての実装
[備考]	Pretranslateで拾った入力情報を書き換えて、後は普通に処理させる。
		いささか原始的な方法だな〜。
[履歴]	2003-12-20	b031228 (3146)タグジャンプキー割り当て(moralog)
-----------------------------------------------------------------------------*/



#include "stdafx.h"
#include "shitarabian.h"
#include "ShitarabianPlugInKeyAssign.h"
#include "PlugInKeyAssignDlg.h"
#include "Common.h"
#include "ExeCommon.h"
#include "SettingPath.h"

#include <afxcview.h>

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


TSystemKeyDefine	f_tSystemKeyDefine[] = 
{

	{ "コピー",					'C',		FALSE,	TRUE,	FALSE },
	{ "切り取り",				'X',		FALSE,	TRUE,	FALSE },
	{ "貼り付け",				'V',		FALSE,	TRUE,	FALSE },
	{ "すべてを選択",			'A',		FALSE,	TRUE,	FALSE },
	{ "選択モード切替",			VK_F4,		FALSE,	FALSE,	FALSE },
	{ "クリップボード履歴",		'B',		FALSE,	TRUE,	FALSE },

	{ "UnDo",					'Z',		FALSE,	TRUE,	FALSE },
	{ "ReDo",					'Y',		FALSE,	TRUE,	FALSE },

	{ "拡大",					VK_ADD,		FALSE,	TRUE,	FALSE },
	{ "縮小",					VK_SUBTRACT,FALSE,	TRUE,	FALSE },
	{ "文字パレット",			' ',		FALSE,	TRUE,	FALSE },

	{ "マーキング",				VK_F2,		FALSE,	TRUE,	FALSE },
	{ "次のマーク",				VK_F2,		FALSE,	FALSE,	FALSE },
	{ "前のマーク",				VK_F2,		TRUE,	FALSE,	FALSE },

	{ "検索ダイアログ",			'F',		FALSE,	TRUE,	FALSE },
	{ "次を検索",				VK_F3,		FALSE,	FALSE,	FALSE },
	{ "前を検索",				VK_F3,		TRUE,	FALSE,	FALSE },
	{ "置換ダイアログ",			'H',		FALSE,	TRUE,	FALSE },

	{ "ページアップ",			VK_PRIOR,	FALSE,	FALSE,	FALSE },
	{ "ページダウン",			VK_NEXT,	FALSE,	FALSE,	FALSE },
	{ "１行アップ",				VK_PRIOR,	FALSE,	TRUE,	FALSE },
	{ "１行ダウン",				VK_NEXT,	FALSE,	TRUE,	FALSE },

	{ "次のペイン",				VK_F6,		FALSE,	FALSE,	FALSE },
	{ "前のぺイン",				VK_F6,		TRUE,	FALSE,	FALSE },

	{ "開く",					'O',		FALSE,	TRUE,	FALSE },
	{ "新規文書",				'N',		FALSE,	TRUE,	FALSE },

	{ "全角空白",				' ',		TRUE,	FALSE,	FALSE },

	{ "左へ１ドットずらす",		VK_LEFT,	TRUE,	TRUE,	TRUE },
	{ "右へ１ドットずらす",		VK_RIGHT,	TRUE,	TRUE,	TRUE },

	// ▼ (3180) BGN 2003.11.18 (dincom)
	{ "コピー",					VK_INSERT,		TRUE,	FALSE,	FALSE },
	{ "切り取り",				VK_DELETE,		TRUE,	FALSE,	FALSE },
	// ▲ (3180) END

// ▼ (3146) BGN 2003.12.20 (moralog) タグジャンプのキー割り当て追加
	{ "タグジャンプ",			VK_F10,		FALSE,	FALSE,	FALSE },
// ▲ (3146) END

};


//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////

CShitarabianPlugInKeyAssign::CShitarabianPlugInKeyAssign()
	: m_bEnable( FALSE )
{
	m_aKeyDefinez.RemoveAll();

	int	idxKeyDefine;
	for( idxKeyDefine=0 ; idxKeyDefine<sizeof(f_tSystemKeyDefine)/sizeof(TSystemKeyDefine) ; idxKeyDefine++ )
	{
		TKeyDefine	tKeyDefine;
		tKeyDefine.bSystemDefine		= TRUE;
		tKeyDefine.strKeyFunctionName	= f_tSystemKeyDefine[idxKeyDefine].szKeyFunctionName;
		tKeyDefine.iSrcKey				= f_tSystemKeyDefine[idxKeyDefine].iSrcKey;
		tKeyDefine.bSrcShift			= f_tSystemKeyDefine[idxKeyDefine].bSrcShift;
		tKeyDefine.bSrcCtrl				= f_tSystemKeyDefine[idxKeyDefine].bSrcCtrl;
		tKeyDefine.bSrcAlt				= f_tSystemKeyDefine[idxKeyDefine].bSrcAlt;
		tKeyDefine.iDstKey				= f_tSystemKeyDefine[idxKeyDefine].iSrcKey;
		tKeyDefine.bDstShift			= f_tSystemKeyDefine[idxKeyDefine].bSrcShift;
		tKeyDefine.bDstCtrl				= f_tSystemKeyDefine[idxKeyDefine].bSrcCtrl;
		tKeyDefine.bDstAlt				= f_tSystemKeyDefine[idxKeyDefine].bSrcAlt;

		m_aKeyDefinez.Add( tKeyDefine );
	}
}

CShitarabianPlugInKeyAssign::~CShitarabianPlugInKeyAssign()
{
	m_aKeyDefinez.RemoveAll();
}

const char* CShitarabianPlugInKeyAssign::GetPlugInName()
{
	return( "キー割り当て ver1.0" );
}

void CShitarabianPlugInKeyAssign::OnSetting(
	CDocumentIF*	pDocIF
)
{
	// warning C4100:警告の無視
	pDocIF;

	CPlugInKeyAssignDlg		dlg;
	dlg.m_aKeyDefinez.RemoveAll();
	dlg.m_aKeyDefinez.Append( m_aKeyDefinez );
	dlg.m_bEnable = m_bEnable;

	if ( dlg.DoModal() == IDOK )
	{
		m_aKeyDefinez.RemoveAll();
		m_aKeyDefinez.Append( dlg.m_aKeyDefinez );
		m_bEnable = dlg.m_bEnable;
	}
}
// キーが押されたときのハンドラ
void CShitarabianPlugInKeyAssign::OnKeyDown(
	CDocumentIF*	pDocIF,
	UCHAR*			punKey,
	BOOL*			pbShift,
	BOOL*			pbCtrl,
	BOOL*			pbAlt
)
{
	// warning C4100:警告の無視
	pDocIF;

	if ( m_bEnable )
	{
		int	idxKeyDefine;
		for( idxKeyDefine=0 ; idxKeyDefine<m_aKeyDefinez.GetSize() ; idxKeyDefine++ )
		{
			if ( *punKey == m_aKeyDefinez[idxKeyDefine].iDstKey )
			if ( (*pbShift?1:0) == (m_aKeyDefinez[idxKeyDefine].bDstShift?1:0) )
			if ( (*pbCtrl?1:0) == (m_aKeyDefinez[idxKeyDefine].bDstCtrl?1:0) )
			if ( (*pbAlt?1:0) == (m_aKeyDefinez[idxKeyDefine].bDstAlt?1:0) )
			{
				*punKey = (UCHAR)m_aKeyDefinez[idxKeyDefine].iSrcKey;
				*pbShift = m_aKeyDefinez[idxKeyDefine].bSrcShift;
				*pbCtrl =m_aKeyDefinez[idxKeyDefine].bSrcCtrl; 
				*pbAlt = m_aKeyDefinez[idxKeyDefine].bSrcAlt;
				return;
			}
		}

		// 元のキー入力を殺す
		for( idxKeyDefine=0 ; idxKeyDefine<m_aKeyDefinez.GetSize() ; idxKeyDefine++ )
		{
			if ( *punKey == m_aKeyDefinez[idxKeyDefine].iSrcKey )
			if ( (*pbShift?1:0) == (m_aKeyDefinez[idxKeyDefine].bSrcShift?1:0) )
			if ( (*pbCtrl?1:0) == (m_aKeyDefinez[idxKeyDefine].bSrcCtrl?1:0) )
			if ( (*pbAlt?1:0) == (m_aKeyDefinez[idxKeyDefine].bSrcAlt?1:0) )
			{
				*punKey = VK_CANCEL;
				*pbShift = FALSE;
				*pbCtrl = FALSE; 
				*pbAlt = FALSE;
				return;
			}
		}
	}
}

// キーが押されたときのハンドラ
void CShitarabianPlugInKeyAssign::OnChar(
	CDocumentIF*	pDocIF,
	CString*		pstrChar,
	BOOL*			pbShift,
	BOOL*			pbCtrl,
	BOOL*			pbAlt
)
{
	if ( m_bEnable )
	if ( !pstrChar->IsEmpty() )
	{
		UCHAR		uChar =  *pstrChar[0];
		OnKeyDown(
			pDocIF,
			&uChar,
			pbShift,
			pbCtrl,
			pbAlt
		);
		if ( *pstrChar[0] != uChar )
		{
			*pstrChar = uChar;
		}
	}
}


void	CShitarabianPlugInKeyAssign::OnLoad(CDocumentIF*	pDocIF)
{
	// warning C4100:警告の無視
	pDocIF;

	static BOOL	bInitialize = FALSE;

	if ( !bInitialize )
	{
		Load( );
		bInitialize = TRUE;
	}

}

void	CShitarabianPlugInKeyAssign::OnSave(CDocumentIF*	pDocIF)
{
	// warning C4100:警告の無視
	pDocIF;

	Save( );
}



void CShitarabianPlugInKeyAssign::Save(
)
{
	try
	{
		CString	strIniFolderPath = CSettingPath::GetInstance()->GetFolderPath();
		CString	strIniFilePath = strIniFolderPath + "keyassign.ini";

		CStdioFile	File( strIniFilePath, CFile::modeCreate|CFile::modeWrite );
		File.WriteString( ::BoolToString( m_bEnable )+"\n" );

		int	idxKeyDefine;
		for( idxKeyDefine=0 ; idxKeyDefine<m_aKeyDefinez.GetSize() ; idxKeyDefine++ )
		{
			File.WriteString( ::BoolToString( m_aKeyDefinez[idxKeyDefine].bSystemDefine )+"\n" );
			File.WriteString( ::QuoteString( m_aKeyDefinez[idxKeyDefine].strKeyFunctionName )+"\n" );
			CString		strDstKey( ::VKToString(m_aKeyDefinez[idxKeyDefine].iDstKey) );
			File.WriteString( ::QuoteString( strDstKey )+"\n" );
			File.WriteString( ::BoolToString( m_aKeyDefinez[idxKeyDefine].bDstShift )+"\n" );
			File.WriteString( ::BoolToString( m_aKeyDefinez[idxKeyDefine].bDstCtrl )+"\n" );
			File.WriteString( ::BoolToString( m_aKeyDefinez[idxKeyDefine].bDstAlt )+"\n" );
			CString		strSrcKey( ::VKToString(m_aKeyDefinez[idxKeyDefine].iSrcKey) );
			File.WriteString( ::QuoteString( strSrcKey )+"\n" );
			File.WriteString( ::BoolToString( m_aKeyDefinez[idxKeyDefine].bSrcShift )+"\n" );
			File.WriteString( ::BoolToString( m_aKeyDefinez[idxKeyDefine].bSrcCtrl )+"\n" );
			File.WriteString( ::BoolToString( m_aKeyDefinez[idxKeyDefine].bSrcAlt )+"\n" );
		}
	}
	catch( CFileException* e )
	{
		e->Delete();
	}
}

void CShitarabianPlugInKeyAssign::Load(
)
{
	try
	{
		CString	strIniFolderPath = CSettingPath::GetInstance()->GetFolderPath();
		CString	strIniFilePath = strIniFolderPath + "keyassign.ini";

		CStdioFile	File( strIniFilePath, CFile::modeRead | CFile::shareDenyNone );
		CString		str;
		BOOL	bExist = TRUE;
		bExist &= File.ReadString( str );
		m_bEnable = bExist && ::StringToBool( str );

		m_aKeyDefinez.RemoveAll();
		while( bExist )
		{
			TKeyDefine	tKeyDefine;
			bExist &= File.ReadString( str );
			tKeyDefine.bSystemDefine = ::StringToBool( str );
			bExist &= File.ReadString( str );
			tKeyDefine.strKeyFunctionName = ::DeQuoteString( str );
			bExist &= File.ReadString( str );
			tKeyDefine.iDstKey = ::StringToVK(::DeQuoteString( str ));
			bExist &= File.ReadString( str );
			tKeyDefine.bDstShift = ::StringToBool( str );
			bExist &= File.ReadString( str );
			tKeyDefine.bDstCtrl = ::StringToBool( str );
			bExist &= File.ReadString( str );
			tKeyDefine.bDstAlt = ::StringToBool( str );
			bExist &= File.ReadString( str );
			tKeyDefine.iSrcKey = ::StringToVK(::DeQuoteString( str ));
			bExist &= File.ReadString( str );
			tKeyDefine.bSrcShift = ::StringToBool( str );
			bExist &= File.ReadString( str );
			tKeyDefine.bSrcCtrl = ::StringToBool( str );
			bExist &= File.ReadString( str );
			tKeyDefine.bSrcAlt = ::StringToBool( str );

			if ( bExist )
			{
				m_aKeyDefinez.Add( tKeyDefine );
			}
		}
	}
	catch( CFileException* e )
	{
		e->Delete();
	}
}
