#define	STRICT
#include	<windows.h>
#include	<windowsx.h>
#include	<commctrl.h>
#include	<commdlg.h>
#include	<tchar.h>
#include	"kctrl.h"
#include	"controls.h"
#include	"fileopen.h"
#include	"basewin.h"
#include	"resource.h"
#include	"appmain.h"
#include	"dialog.h"

extern "C" {
	extern	DWORD	g_dwTabSize ;
	extern	DWORD	g_dwHMargin ;
	extern	BOOL	g_bUseClipboard ;
	extern	TCHAR	g_szEditor[ MAX_PATH ] ;
	extern	TCHAR	g_szPadRepl[ MAX_PATH ] ;
	extern	TCHAR	g_szPadNew[ MAX_PATH ] ;
	extern	TCHAR	g_szPadMail[ MAX_PATH ] ;
	extern	TCHAR	g_szQuote[ 256 ] ;
	extern	TCHAR	g_szPadDir[ MAX_PATH ] ;
	extern	TCHAR	g_szMailPadFn[ MAX_PATH ] ;
	extern	TCHAR	g_szForumPadFn[ MAX_PATH ] ;
	extern	TCHAR	g_szFindStr[ 128 ] ;
	extern	BOOL	g_bFindDirNext ;
	extern	BOOL	g_bIgnoreCase ;

	extern	void	ShowSIP( BOOL bShow ) ;
}

static	HINSTANCE	g_hInst = 0 ;

/*
 * リストボックス用の固定ピッチフォントを作成する
 */
HFONT
CreateListboxFont()
{
	LOGFONT         lf ;

	/* フォントを取得する */
	memset( &lf, 0, sizeof lf ) ;
	lf.lfPitchAndFamily = FIXED_PITCH | FF_MODERN ;
	lf.lfCharSet = 128 ;
	lf.lfHeight = 13 ;
	return CreateFontIndirect( &lf ) ;
}

BOOL
OpenFn( HINSTANCE hInst, HWND hWnd, LPTSTR buf, LPCTSTR ext, LPCTSTR filter )
{
	OPENFILENAME	ofn ;

	memset( &ofn, 0, sizeof ofn ) ;
	_tcscpy( buf, TEXT("*.") ) ;
	_tcscat( buf, ext ) ;
	ofn.lStructSize = sizeof ofn ;
	ofn.hwndOwner = hWnd ;
	ofn.hInstance = hInst ;
	ofn.lpstrFilter = filter ;
	ofn.lpstrCustomFilter = NULL ;
	ofn.nMaxCustFilter = 0 ;
	ofn.nFilterIndex = 1 ;
	ofn.lpstrFile = buf ;
	ofn.nMaxFile = MAX_PATH ;
	ofn.lpstrFileTitle = NULL ;
	ofn.nMaxFileTitle = 0 ;
	ofn.lpstrInitialDir = NULL ;
	ofn.lpstrTitle = NULL ;
	ofn.Flags = 0 ;
	ofn.nFileOffset = 0 ;
	ofn.nFileExtension = 0 ;
	ofn.lpstrDefExt = ext ;
	ofn.lCustData = 0 ;
	ofn.lpfnHook = NULL ;
	ofn.lpTemplateName = NULL ;
	if ( !GetOpenFileName( &ofn ) ) {
		return FALSE ;
	}
	return TRUE ;
}

void
WMDrawItem( HINSTANCE hInst, LPDRAWITEMSTRUCT lpdis )
{
	RECT		rect ;
	HDC			hdcCom ;
	HBITMAP		hbmpFolder, hbmpOld ;

	hbmpFolder = LoadBitmap( g_hInst, MAKEINTRESOURCE(IDB_FOLDER) ) ;
	hdcCom = CreateCompatibleDC( lpdis->hDC ) ;
	if ( hdcCom ) {
		hbmpOld = SelectBitmap( hdcCom, hbmpFolder ) ;
		GetClientRect( lpdis->hwndItem, &rect ) ;
		FillRect( lpdis->hDC, &rect, (HBRUSH) GetStockObject( WHITE_BRUSH ) ) ;
		if ( lpdis->itemState & ODS_SELECTED ) {
			DrawEdge( lpdis->hDC, &rect, EDGE_SUNKEN, BF_RECT ) ;
			BitBlt( lpdis->hDC, 3, 3, 15, 13, hdcCom, 0, 0, SRCCOPY ) ;
		} else {
			DrawEdge( lpdis->hDC, &rect, EDGE_RAISED, BF_RECT ) ;
			BitBlt( lpdis->hDC, 2, 2, 15, 13, hdcCom, 0, 0, SRCCOPY ) ;
		}
		SelectBitmap( hdcCom, hbmpOld ) ;
		DeleteDC( hdcCom ) ;
	}
	DeleteObject( hbmpFolder ) ;
}

BOOL APIENTRY
GeneralDlgProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
	NMHDR		*nmhdr ;
	TCHAR		buf[ MAX_PATH ] ;

	switch ( message ) {
	case WM_INITDIALOG:
		SetDlgItemInt( hDlg, IDC_TABSIZE, g_dwTabSize, FALSE ) ;
		SetDlgItemInt( hDlg, IDC_HMARGIN, g_dwHMargin, FALSE ) ;
		SendDlgItemMessage( hDlg, IDC_CHK_USECB, BM_SETCHECK, g_bUseClipboard ? BST_CHECKED : BST_UNCHECKED, 0 ) ;
		SetDlgItemText( hDlg, IDC_EDITOR, g_szEditor ) ;
		SetDlgItemText( hDlg, IDC_PADDIR, g_szPadDir ) ;
		SetDlgItemText( hDlg, IDC_MAILPADFN, g_szMailPadFn ) ;
		SetDlgItemText( hDlg, IDC_FORUMPADFN, g_szForumPadFn ) ;
		return TRUE ;
	case WM_DRAWITEM:
		switch ( (UINT) wParam ) {
		case IDC_BTN_EDITOR:
		case IDC_BTN_NEW:
		case IDC_BTN_REPLY:
		case IDC_BTN_MAIL:
			WMDrawItem( g_hInst, (LPDRAWITEMSTRUCT) lParam ) ;
			break ;
		}
		break ;
	case WM_COMMAND:
		switch ( GET_WM_COMMAND_ID( wParam, lParam ) ) {
		case IDC_EDITOR:
		case IDC_PADDIR:
		case IDC_MAILPADFN:
		case IDC_FORUMPADFN:
			if ( GET_WM_COMMAND_CMD( wParam, lParam ) == EN_SETFOCUS ) {
				ShowSIP( TRUE ) ;
			}
			break ;
		case IDC_BTN_EDITOR:
			if ( OpenFn( g_hInst, hDlg, buf, TEXT("EXE"), TEXT("Exe Files (*.EXE)\0*.EXE\0\0") ) ) {
				SetDlgItemText( hDlg, IDC_EDITOR, buf ) ;
			}
			break ;
		}
		break ;
	case WM_NOTIFY:
		nmhdr = (NMHDR*) lParam ;
		switch ( nmhdr->code ) {
		case PSN_KILLACTIVE:
			if ( GetDlgItemInt( hDlg, IDC_TABSIZE, NULL, FALSE ) == 0 ) {
				SetWindowLong( hDlg, DWL_MSGRESULT, TRUE ) ;
				return TRUE ;
			}
			break ;
		case PSN_APPLY:
			/* タブサイズを取得する */
			g_dwTabSize = GetDlgItemInt( hDlg, IDC_TABSIZE, NULL, FALSE ) ;
			/* HMarginサイズを取得する */
			g_dwHMargin = GetDlgItemInt( hDlg, IDC_HMARGIN, NULL, FALSE ) ;
			/* UseClipboardフラグを取得する */
			g_bUseClipboard = SendDlgItemMessage( hDlg, IDC_CHK_USECB, BM_GETCHECK, 0, 0 ) == BST_CHECKED ;
			/* エディタープログラム名を取得する */
			GetDlgItemText( hDlg, IDC_EDITOR, g_szEditor, sizeof g_szEditor / sizeof *g_szEditor ) ;
			/* PADディレクトリを取得する */
			GetDlgItemText( hDlg, IDC_PADDIR, g_szPadDir, sizeof g_szPadDir / sizeof *g_szPadDir ) ;
			/* MAIL-PADファイル名を取得する */
			GetDlgItemText( hDlg, IDC_MAILPADFN, g_szMailPadFn, sizeof g_szMailPadFn / sizeof *g_szMailPadFn ) ;
			/* FORUM-PADファイル名を取得する */
			GetDlgItemText( hDlg, IDC_FORUMPADFN, g_szForumPadFn, sizeof g_szForumPadFn / sizeof *g_szForumPadFn ) ;
			break ;
		}
		break ;
	}
	return FALSE ;
}

BOOL APIENTRY
TemplateDlgProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
	NMHDR		*nmhdr ;
	TCHAR		buf[ MAX_PATH ] ;

	switch ( message ) {
	case WM_INITDIALOG:
		SetDlgItemText( hDlg, IDC_QUOTE, g_szQuote ) ;
		SetDlgItemText( hDlg, IDC_PADREPL, g_szPadRepl ) ;
		SetDlgItemText( hDlg, IDC_PADNEW, g_szPadNew ) ;
		SetDlgItemText( hDlg, IDC_PADMAIL, g_szPadMail ) ;
		return TRUE ;
	case WM_DRAWITEM:
		switch ( (UINT) wParam ) {
		case IDC_BTN_EDITOR:
		case IDC_BTN_NEW:
		case IDC_BTN_REPLY:
		case IDC_BTN_MAIL:
			WMDrawItem( g_hInst, (LPDRAWITEMSTRUCT) lParam ) ;
			break ;
		}
		break ;
	case WM_COMMAND:
		switch ( GET_WM_COMMAND_ID( wParam, lParam ) ) {
		case IDC_PADNEW:
		case IDC_PADREPL:
		case IDC_PADMAIL:
			if ( GET_WM_COMMAND_CMD( wParam, lParam ) == EN_SETFOCUS ) {
				ShowSIP( TRUE ) ;
			}
			break ;
		case IDC_BTN_NEW:
			if ( OpenFn( g_hInst, hDlg, buf, TEXT("TXT"), TEXT("Text Files (*.TXT)\0*.TXT\0\0") ) ) {
				SetDlgItemText( hDlg, IDC_PADNEW, buf ) ;
			}
			break ;
		case IDC_BTN_REPLY:
			if ( OpenFn( g_hInst, hDlg, buf, TEXT("TXT"), TEXT("Text Files (*.TXT)\0*.TXT\0\0") ) ) {
				SetDlgItemText( hDlg, IDC_PADREPL, buf ) ;
			}
			break ;
		case IDC_BTN_MAIL:
			if ( OpenFn( g_hInst, hDlg, buf, TEXT("TXT"), TEXT("Text Files (*.TXT)\0*.TXT\0\0") ) ) {
				SetDlgItemText( hDlg, IDC_PADMAIL, buf ) ;
			}
			break ;
		}
		break ;
	case WM_NOTIFY:
		nmhdr = (NMHDR*) lParam ;
		switch ( nmhdr->code ) {
		case PSN_APPLY:
			/* 引用記号を取得する */
			GetDlgItemText( hDlg, IDC_QUOTE, g_szQuote, sizeof g_szQuote / sizeof *g_szQuote ) ;
			/* 返信用PADテンプレートを取得する */
			GetDlgItemText( hDlg, IDC_PADREPL, g_szPadRepl, sizeof g_szPadRepl / sizeof *g_szPadRepl ) ;
			/* 新規用PADテンプレートを取得する */
			GetDlgItemText( hDlg, IDC_PADNEW, g_szPadNew, sizeof g_szPadNew / sizeof *g_szPadNew ) ;
			/* 新規用PADテンプレートを取得する */
			GetDlgItemText( hDlg, IDC_PADMAIL, g_szPadMail, sizeof g_szPadMail / sizeof *g_szPadMail ) ;
			break ;
		}
	}
	return FALSE ;
}

BOOL
SetupDialogExec( HINSTANCE hInst, HWND hWnd, BOOL bNarrow, LPDWORD pResult )
{
	PROPSHEETPAGE	psp ;
	PROPSHEETHEADER	psh ;
	HPROPSHEETPAGE	hpsp[ 2 ] ;
	DWORD			ret, dwTabSize, dwHMargin ;

	g_hInst = hInst ;
	dwTabSize = g_dwTabSize ;
	dwHMargin = g_dwHMargin ;

	psp.dwSize = sizeof (PROPSHEETPAGE) ;
	psp.dwFlags = PSP_DEFAULT ;
	psp.hInstance = hInst ;

	psp.pszTemplate = bNarrow ? MAKEINTRESOURCE(IDD_SCLD_GENERAL) : MAKEINTRESOURCE(IDD_LCLD_GENERAL) ;
	psp.pfnDlgProc = (DLGPROC) GeneralDlgProc ;
	hpsp[ 0 ] = CreatePropertySheetPage( &psp ) ;

	psp.pszTemplate = bNarrow ? MAKEINTRESOURCE(IDD_SCLD_TEMPLATE) : MAKEINTRESOURCE(IDD_LCLD_TEMPLATE) ;
	psp.pfnDlgProc = (DLGPROC) TemplateDlgProc ;
	hpsp[ 1 ] = CreatePropertySheetPage( &psp ) ;

	memset( &psh, 0, sizeof (PROPSHEETHEADER) ) ;
	psh.dwSize = sizeof (PROPSHEETHEADER) ;
	psh.dwFlags = PSH_PROPTITLE ;
	psh.dwFlags = PSH_DEFAULT ;
	psh.hwndParent = hWnd ;
	psh.hInstance = hInst ;
	psh.pszCaption = TEXT("KLog Properties") ;
	psh.nPages = 2 ;
	psh.nStartPage = 0 ;
	psh.phpage = hpsp ;
	/* プロパティシートを表示する */
	ret = PropertySheet( &psh ) ;
	if ( ret == -1 ) {
		return FALSE ;
	}

	/* SIPをオフにする */
	ShowSIP( FALSE ) ;
	/* 画面構成の変更チェック */
	ret = SETUP_RET_NORMAL ;
	if ( (g_dwTabSize != dwTabSize) || (g_dwHMargin != dwHMargin) ) {
		/* 画面構成の変更が必要 */
		ret |= SETUP_RET_CHANGE ;
	}
	*pResult = ret ;
	return TRUE ;
}

static	BOOL APIENTRY
FindDlgProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
	HWND		hCtrl ;
	TCHAR		buf[ 128 ] ;

	switch ( message ) {
	case WM_INITDIALOG:
		SendDlgItemMessage( hDlg, IDC_CHK_IGNORECASE, BM_SETCHECK, (WPARAM) g_bIgnoreCase ? BST_CHECKED : BST_UNCHECKED, 0 ) ;
		hCtrl = GetDlgItem( hDlg, IDC_FINDSTR ) ;
		SetWindowText( hCtrl, g_szFindStr ) ;
		SendMessage( hCtrl, EM_SETSEL, (WPARAM) 0, (LPARAM) -1 ) ;
		SetFocus( hCtrl ) ;
		break ;
	case WM_COMMAND:
		switch ( GET_WM_COMMAND_ID( wParam, lParam ) ) {
		case IDC_BTN_NEXT:
			*buf = 0 ;
			GetDlgItemText( hDlg, IDC_FINDSTR, buf, 128 ) ;
			if ( !*buf ) {
				MessageBeep( 0xFFFFFFFF ) ;
				return FALSE ;
			}
			_tcscpy( g_szFindStr, buf ) ;
			g_bFindDirNext = GET_WM_COMMAND_ID( wParam, lParam ) == IDC_BTN_NEXT ;
			g_bIgnoreCase = SendDlgItemMessage( hDlg, IDC_CHK_IGNORECASE, BM_GETCHECK, 0, 0 ) == BST_CHECKED ;
			EndDialog( hDlg, TRUE ) ;
			return TRUE ;
		case IDCANCEL:
			EndDialog( hDlg, FALSE ) ;
			return TRUE ;
		}
		break ;
	}
	return FALSE ;
}

BOOL
FindDialogExec( HINSTANCE hInst, HWND hWnd )
{
	return DialogBox( hInst, MAKEINTRESOURCE(IDD_DLG_FIND), hWnd, FindDlgProc ) ;
}

BOOL CALLBACK
AboutDlgFunc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
	TCHAR	buf[ 128 ], *ptr ;

	switch ( message ) {
	case WM_INITDIALOG:
		/* ダイアログタイトル */
		_tcscpy( buf, TEXT("About ") ) ;
		_tcscat( buf, szTitleName ) ;
		SetWindowText( hDlg, buf ) ;
		/* アプリケーションバージョン */
		_tcscpy( buf, szTitleName ) ;
		_tcscat( buf, TEXT(" for WinCE V") ) ;
		ptr = &buf[ _tcslen( buf ) ] ;
		*ptr++ = TEXT('0') + (TCHAR) (g_dwAppVersion / 100) ;
		*ptr++ = TEXT('.') ;
		*ptr++ = TEXT('0') + (TCHAR) ((g_dwAppVersion / 10) % 10) ;
		*ptr++ = TEXT('0') + (TCHAR) (g_dwAppVersion % 10) ;
		*ptr = 0 ;
		SetDlgItemText( hDlg, IDC_VERSION, buf ) ;
		/* DLLバージョン */
		_tcscpy( buf, TEXT("KCTRL Ver ") ) ;
		ptr = &buf[ _tcslen( buf ) ] ;
		*ptr++ = TEXT('0') + (TCHAR) (g_dwDllVersion / 100) ;
		*ptr++ = TEXT('.') ;
		*ptr++ = TEXT('0') + (TCHAR) ((g_dwDllVersion / 10) % 10) ;
		*ptr++ = TEXT('0') + (TCHAR) (g_dwDllVersion % 10) ;
		*ptr = 0 ;
		SetDlgItemText( hDlg, IDC_DLLVERSION, buf ) ;
		/* Copyright */
		_tcscpy( buf, TEXT("Copyright (C) 1999-2000\r\n") ) ;
		_tcscat( buf, TEXT("by Eiichiro ITO\r\n") ) ;
		_tcscat( buf, TEXT("All Rights Reserved") ) ;
		SetDlgItemText( hDlg, IDC_COPYRIGHT, buf ) ;
		return TRUE ;
	case WM_COMMAND:
		switch ( GET_WM_COMMAND_ID( wParam, lParam ) ) {
		case IDOK:
		case IDCANCEL:
			EndDialog( hDlg, TRUE ) ;
			return TRUE ;
		case IDC_UNINSTALL:
			if ( MessageBox( hDlg, TEXT("Uninstall Application?"), TEXT("Uninstal"), MB_YESNO|MB_ICONQUESTION ) == IDYES ) {
				g_bDeleteReg = TRUE ;
				EndDialog( hDlg, FALSE ) ;
			}
			break ;
		}
		break ;
	}
	return FALSE ;
}
