// ★コメント未記入
// ShitarabianPlugInDll.cpp: CShitarabianPlugInDll クラスのインプリメンテーション
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "shitarabian.h"
#include "ShitarabianPlugInDll.h"

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

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

CShitarabianPlugInDll::CShitarabianPlugInDll()
	: m_hDll( NULL )
	, m_OnInitialize( NULL )
	, m_OnDestroy( NULL )
	, m_GetPlugInName( NULL )
	, m_OnDrawWall( NULL )
	, m_OnEdit( NULL )
	, m_OnSetting( NULL )
	, m_OnToolButton( NULL )
	, m_OnToolButtonUI( NULL )
	, m_OnKeyDown( NULL )
	, m_OnChar( NULL )

{

}

CShitarabianPlugInDll::~CShitarabianPlugInDll()
{
	Free();
}

void		CShitarabianPlugInDll::OnInitialize(
	CDocumentIF*	pDocIF
)
{
	if ( m_OnInitialize != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnInitialize( pDocIF );

	    ::AfxSetResourceHandle(hInstance);
	}
}

void		CShitarabianPlugInDll::OnDestroy(
	CDocumentIF*	pDocIF
)
{
	if ( m_OnDestroy != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnDestroy( pDocIF );

	    ::AfxSetResourceHandle(hInstance);
	}
}


void		CShitarabianPlugInDll::OnLoad(
	CDocumentIF*	pDocIF
)
{
	if ( m_OnLoad != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnLoad( pDocIF );

	    ::AfxSetResourceHandle(hInstance);
	}
}

void		CShitarabianPlugInDll::OnSave(
	CDocumentIF*	pDocIF
)
{
	if ( m_OnSave != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnSave( pDocIF );

	    ::AfxSetResourceHandle(hInstance);
	}
}
const char*		CShitarabianPlugInDll::GetPlugInName()
{
	const char*		pName = NULL;
	if ( m_GetPlugInName != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_GetPlugInName(
			&pName
		);

	    ::AfxSetResourceHandle(hInstance);
	}
	
	return( pName );
}


// 背景を表示するときのフック
void		CShitarabianPlugInDll::OnDrawWall(
	CDocumentIF*	pDocIF,
	CDC*			pDstDC,
	int				iLeft,
	int				iTop,
	int				iWidth,
	int				iHeight
)
{
	if ( m_OnDrawWall != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnDrawWall(
			pDocIF,
			pDstDC->m_hDC,
			iLeft,
			iTop,
			iWidth,
			iHeight
		);

	    ::AfxSetResourceHandle(hInstance);
	}
}

	// ドキュメントの内容が編集されたときのフック
void		CShitarabianPlugInDll::OnEdit(
		CDocumentIF*	pDocIF
)
{
	if ( m_OnEdit != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnEdit(
			pDocIF
		);

	    ::AfxSetResourceHandle(hInstance);
	}
}

	// 設定ダイアログ
void		CShitarabianPlugInDll::OnSetting(
		CDocumentIF*	pDocIF
)
{
	if ( m_OnSetting != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnSetting(
			pDocIF
		);

	    ::AfxSetResourceHandle(hInstance);

	}
}


BOOL	CShitarabianPlugInDll::Load(
	CString&	strFilePath
)
{
	m_hDll = ::AfxLoadLibrary( strFilePath );

	if ( m_hDll != NULL )
	{
		// 関数ポインタの取得
		m_OnInitialize = (ONINITIALIZE)GetProcAddress(
			m_hDll,
			"OnInitialize"
		);

		m_OnDestroy = (ONDESTROY)GetProcAddress(
			m_hDll,
			"OnDestroy"
		);

		m_OnLoad = (ONLOAD)GetProcAddress(
			m_hDll,
			"OnLoad"
		);

		m_OnSave = (ONSAVE)GetProcAddress(
			m_hDll,
			"OnSave"
		);

		m_GetPlugInName = (GETPLUGINNAME)GetProcAddress(
			m_hDll,
			"GetPlugInName"
		);

		m_OnDrawWall = (ONDRAWWALL)GetProcAddress(
			m_hDll,
			"OnDrawWall"
		);

		m_OnEdit = (ONEDIT)GetProcAddress(
			m_hDll,
			"OnEdit"
		);

		m_OnSetting = (ONSETTING)GetProcAddress(
			m_hDll,
			"OnSetting"
		);

		m_OnToolButton = (ONTOOLBUTTON)GetProcAddress(
			m_hDll,
			"OnToolButton"
		);

		m_OnToolButtonUI = (ONTOOLBUTTONUI)GetProcAddress(
			m_hDll,
			"OnToolButtonUI"
		);

		m_OnKeyDown = (ONKEYDOWN)GetProcAddress(
			m_hDll,
			"OnKeyDown"
		);

		m_OnChar = (ONCHAR)GetProcAddress(
			m_hDll,
			"OnChar"
		);
	}

	return( TRUE );
}


BOOL	CShitarabianPlugInDll::Free()
{
	if ( m_hDll != NULL)
	{
		::AfxFreeLibrary( m_hDll );
		m_hDll = NULL;
	}
	return( TRUE );
}
void CShitarabianPlugInDll::OnToolButton(
	CDocumentIF*	pDocIF,
	int				idxButton
)
{
	if ( m_OnToolButton != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnToolButton(
			pDocIF,
			idxButton
		);

	    ::AfxSetResourceHandle(hInstance);
	}
}
void CShitarabianPlugInDll::OnToolButtonUI(
	CDocumentIF*	pDocIF,
	int				idxButton,
	CCmdUI*			pCmdUI
)
{
	if ( m_OnToolButtonUI != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnToolButtonUI(
			pDocIF,
			idxButton,
			pCmdUI
		);

	    ::AfxSetResourceHandle(hInstance);
	}
}

// キーが押されたときのハンドラ
void	CShitarabianPlugInDll::OnKeyDown(
	CDocumentIF*	pDocIF,
	UCHAR*			punKey,
	BOOL*			pbShift,
	BOOL*			pbCtrl,
	BOOL*			pbAlt
)
{
	if ( m_OnKeyDown != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnKeyDown(
			pDocIF,
			punKey,
			pbShift,
			pbCtrl,
			pbAlt
		);

	    ::AfxSetResourceHandle(hInstance);
	}
}

void	CShitarabianPlugInDll::OnChar(
	CDocumentIF*	pDocIF,
	CString*		pstrChar,
	BOOL*			pbShift,
	BOOL*			pbCtrl,
	BOOL*			pbAlt
)
{
	if ( m_OnChar != NULL )
	{
	    const HINSTANCE hInstance = ::AfxGetResourceHandle();
		::AfxSetResourceHandle( m_hDll );

		m_OnChar(
			pDocIF,
			pstrChar,
			pbShift,
			pbCtrl,
			pbAlt
		);

	    ::AfxSetResourceHandle(hInstance);
	}
}