#define	STRICT
#include	<windows.h>
#include	<commctrl.h>
#include	<tchar.h>
#include	"kctrl.h"
#include	"appmain.h"

#define		VALID_KCTRL_VERSION		(27)

DWORD		g_dwAppVersion = 10 ;
DWORD		g_dwDllVersion = 0 ;
LPCTSTR		g_szArgs = 0 ;
HINSTANCE	g_hLibrary = 0 ;
BOOL		(*SHSipInfo)( UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni ) = 0 ;

int WINAPI
#ifdef	_WIN32_WCE
WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst,
		 LPTSTR lpszArgs, int nWinMode )
#else	/* _WIN32_WCE */
WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst,
		 LPSTR lpszArgs, int nWinMode )
#endif	/* _WIN32_WCE */
{
	HWND	hWnd ;

	/* 既に起動されているかどうかチェックする */
	hWnd = FindWindow( szTitleName, NULL ) ;
	if ( hWnd != NULL ) {
		/* 起動済みの方をアクティブにする */
		SetForegroundWindow( hWnd ) ;
		return TRUE ;
	}

	InitCommonControls() ;

	/* KCTRL.DLLの初期化を行う */
	if ( !InitKanjiControls() ) {
		_tcscpy( MessageBuf, TEXT("KCTRL.DLL Initialize error") ) ;
		MessageBox( NULL, MessageBuf, TEXT("KCTRL Error"), MB_OK|MB_ICONEXCLAMATION ) ;
		return -1 ;
	}
	g_dwDllVersion = GetKVersion() ;

	/* KCTRLのバージョンチェックを行う */
	if ( g_dwDllVersion < VALID_KCTRL_VERSION ) {
		wsprintf( MessageBuf,
				  TEXT("Obsolete KCTRL.DLL\r\nPlease use Ver%d.%02d or later"),
				  VALID_KCTRL_VERSION / 100, VALID_KCTRL_VERSION % 100 ) ;
		MessageBox( NULL, MessageBuf, TEXT("KCTRL Error"), MB_OK|MB_ICONEXCLAMATION ) ;
		return -1 ;
	}

#ifdef	_WIN32_WCE
	g_szArgs = lpszArgs ;
#endif	/* _WIN32_WCE */

#ifdef	_WIN32_WCE
	g_hLibrary = LoadLibrary( TEXT("AYGSHELL.DLL") ) ;
	if ( g_hLibrary ) {
		SHSipInfo = (BOOL (*)(UINT,UINT,PVOID,UINT)) GetProcAddress( g_hLibrary, TEXT("SHSipInfo") ) ;
	}
#endif	/* _WIN32_WCE */

	BOOL		exitVal = FALSE ;
	MainApp		*pApp = new MainApp() ;
	if ( !pApp ) {
		goto exit ;
	}
	if ( pApp->Create( hThisInst ) ) {
		exitVal = pApp->Run() ;
	}
	delete pApp ;

exit:
	ReleaseKanjiControls() ;
	return exitVal ;
}

void
ShowSIP( BOOL bShow )
{
	SIPINFO		si ;

	memset( &si, 0, sizeof si ) ;
	si.cbSize = sizeof si ;
	if ( SHSipInfo && SHSipInfo( SPI_GETSIPINFO, 0, &si, 0 ) ) {
		if ( bShow ) {
			si.fdwFlags |= SIPF_ON ;
		} else {
			si.fdwFlags &= ~SIPF_ON ;
		}
		SHSipInfo( SPI_SETSIPINFO, 0, &si, 0 ) ;
	}
}
