#define	STRICT
#include	<windows.h>
#include	<tchar.h>
#include	"fepbox.h"

#define		GawaroBaseKey			TEXT("Software\\Gawaro")
#define		VAR_FEPNAME				TEXT("FepName")
#define		VAR_FEPPROGRAM			TEXT("FepProgram")
#define		VAR_USECTRL				TEXT("UseCtrl")

TCHAR		g_szFepName[ MAX_PATH ] = TEXT("KANJIIN") ;
TCHAR		g_szFepProgram[ MAX_PATH ] = TEXT("KanjiIn.exe") ;
BOOL		g_fKanji = FALSE ;
BOOL		g_fUseCtrl = FALSE ;
BOOL		g_fFepOption = FALSE ;

/*
 * FEPを初期化する
 */
BOOL
InitFep( void )
{
	HKEY	hk ;
	LONG	lret ;
	LPCTSTR	name ;
	LPBYTE	lpData ;
	TCHAR	sValue[ 256 ] ;
	DWORD	dwType, cbData, dwValue ;

	/* レジストリキーをオープンする */
	lret = RegOpenKeyEx( HKEY_CURRENT_USER, GawaroBaseKey, 0,
						 KEY_QUERY_VALUE, &hk ) ;
	if ( lret == ERROR_SUCCESS ) {
		/* FEP名 */
		name = VAR_FEPNAME ;
		dwType = REG_SZ ;
		lpData = (LPBYTE) sValue ;
		cbData = sizeof sValue ;
		if ( RegQueryValueEx( hk, name, NULL, &dwType, lpData, &cbData ) == ERROR_SUCCESS ) {
			_tcscpy( g_szFepName, sValue ) ;
		}
		/* FEPプログラム名 */
		name = VAR_FEPPROGRAM ;
		dwType = REG_SZ ;
		lpData = (LPBYTE) sValue ;
		cbData = sizeof sValue ;
		if ( RegQueryValueEx( hk, name, NULL, &dwType, lpData, &cbData ) == ERROR_SUCCESS ) {
			_tcscpy( g_szFepProgram, sValue ) ;
		}
		/* CTRL/ALT切り替え */
		name = VAR_USECTRL ;
		dwType = REG_DWORD ;
		lpData = (LPBYTE) &dwValue ;
		cbData = sizeof dwValue ;
		if ( RegQueryValueEx( hk, name, NULL, &dwType, lpData, &cbData ) == ERROR_SUCCESS ) {
			g_fUseCtrl = dwValue ;
		}
		/* レジストリクローズ */
		RegCloseKey( hk ) ;
	}
	return TRUE ;
}

/*
 * FEPを起動する
 */
void
Fep_Execute( HWND hWnd )
{
	HWND	hWndFep ;

	hWndFep = FindWindow( g_szFepName, NULL ) ;
	if ( hWndFep ) {
		/* FEPがあった場合は現在の逆の状態にする */
		g_fKanji = SendMessage( hWndFep, WM_COMMAND, IDM_FEPGETMODE, 0 ) ;
		SendMessage( hWndFep, WM_COMMAND, IDM_FEPSETMODE, g_fKanji ? 0 : 1 ) ;
#ifdef	_WIN32_WCE
	} else {
		/* FEPがなかった場合 */
		PROCESS_INFORMATION	pi ;
		TCHAR				param[ 20 ] ;
		wsprintf( param, TEXT("%d"), hWnd ) ;
		g_fKanji = CreateProcess( g_szFepProgram, param,
						NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi ) ;
#endif	/* _WIN32_WCE */
	}
}

/*
 * WM_CHARの処理を行う
 */
BOOL
Fep_WM_CHAR( HWND hWnd, TCHAR chCharCode, LONG lKeyData )
{
	HWND	hWndFep ;

	if ( chCharCode == TEXT(' ') ) {
		if ( g_fUseCtrl && (GetKeyState( VK_CONTROL) & 0x80) ) {
			/* CTRL+SPACEが押された場合 */
			Fep_Execute( hWnd ) ;
			return TRUE ;
		}
		return FALSE ;
	} else if ( chCharCode < TEXT(' ') ) {
		return FALSE ;
	}
	hWndFep = FindWindow( g_szFepName, NULL ) ;
	if ( hWndFep ) {
		g_fKanji = SendMessage( hWndFep, WM_COMMAND, IDM_FEPGETMODE, 0 ) ? FALSE : TRUE ;
	} else {
		g_fKanji = FALSE ;
	}
	if ( g_fKanji && (chCharCode != TEXT(' ')) ) {
		SendMessage( hWndFep, WM_COMMAND, MAKEWPARAM( IDM_ACTIVATE_HWND, chCharCode ), (LPARAM) hWnd ) ;
		return TRUE ;
	}
	return FALSE ;
}

/*
 * WM_SYSCHARの処理を行う
 */
BOOL
Fep_WM_SYSCHAR( HWND hWnd, TCHAR chCharCode, LONG lKeyData )
{
	if ( chCharCode == TEXT(' ') ) {
		if ( !g_fUseCtrl && (lKeyData & 0x20000000) ) {
			/* ALT+SPACEが押された場合 */
			Fep_Execute( hWnd ) ;
			return TRUE ;
		}
	}
	return FALSE ;
}

BOOL
Fep_WM_ACTIVATE( HWND hWnd, WORD fActive, BOOL fMinimize, HWND hWndDeactive )
{
	if ( g_fFepOption ) {
		if ( fActive == WA_ACTIVE || fActive == WA_CLICKACTIVE ) {
			HWND	hWndFep = FindWindow( g_szFepName, NULL ) ;
			if ( hWndFep ) {
				g_fKanji = SendMessage( hWndFep, WM_COMMAND, IDM_FEPGETMODE, 0 ) ? FALSE : TRUE ;
				if ( g_fKanji ) {
					SendMessage( hWndFep, WM_COMMAND, IDM_FEPSETMODE, 1 ) ;
				}
			}
		}
	}
	return FALSE ;
}
