// LaunchItemDlg.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "shitarabian.h"
#include "LaunchItemDlg.h"
#include <stdlib.h>
#include "Launcher.h"
#include "Common.h"
#include "ExeCommon.h"


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


/////////////////////////////////////////////////////////////////////////////
// CLaunchItemDlg ダイアログ


CLaunchItemDlg::CLaunchItemDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLaunchItemDlg::IDD, pParent)
	, m_pLaunchItem( NULL )
	, m_idxIcon( 0 )
	, m_strIconPathName()
{
	//{{AFX_DATA_INIT(CLaunchItemDlg)
	m_strName = _T("");
	m_strCommand = _T("");
	m_bEnable = FALSE;
	m_bDefaultOpen = FALSE;
	//}}AFX_DATA_INIT
}


void CLaunchItemDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLaunchItemDlg)
	DDX_Control(pDX, IDC_LAUNCHITEM_COMMAND, m_editCommand);
	DDX_Control(pDX, IDC_LAUNCHITEM_ICON, m_IconCanvas);
	DDX_Text(pDX, IDC_LAUNCHITEM_NAME, m_strName);
	DDX_Text(pDX, IDC_LAUNCHITEM_COMMAND, m_strCommand);
	DDX_Check(pDX, IDC_LAUNCHITEM_ENABLE, m_bEnable);
	DDX_Check(pDX, IDC_LAUNCHITEM_DEFAULT_OPEN, m_bDefaultOpen);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLaunchItemDlg, CDialog)
	//{{AFX_MSG_MAP(CLaunchItemDlg)
	ON_BN_CLICKED(IDC_LAUNCHITEM_ICONREF, OnLaunchitemIconref)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_LAUNCHITEM_PATH, OnLaunchitemPath)
	ON_BN_CLICKED(IDC_LAUNCHITEM_SHITARABIANFOLDER, OnLaunchitemShitarabianfolder)
	ON_BN_CLICKED(IDC_LAUNCHITEM_SHITARABIANHANDLE, OnLaunchitemShitarabianhandle)
	ON_BN_CLICKED(IDC_LAUNCHITEM_FILEREF, OnLaunchitemFileref)
	ON_BN_CLICKED(IDC_LAUNCHITEM_SHITARABIANCURENTDOC, OnLaunchitemShitarabiancurentdoc)
	ON_BN_CLICKED(IDC_LAUNCHITEM_SHITARABIANSELECTION, OnLaunchitemShitarabianselection)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// OSがUnicodeを使用しているかを判定する
BOOL IsUnicodeSystem()
{
    // OS のバージョン情報を取得する
    OSVERSIONINFO stOsVerInfo;
    stOsVerInfo.dwOSVersionInfoSize = sizeof(stOsVerInfo);
    if ( ::GetVersionEx( &stOsVerInfo ) )
	{
        if ( stOsVerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT )
		{
            // NT 系
            return( TRUE );
        }
		else
		{
		    // NT 系ではない
            return( FALSE );
        }
    }
	else
	{
		// 失敗
		return( FALSE );
    }
}


/////////////////////////////////////////////////////////////////////////////
// CLaunchItemDlg メッセージ ハンドラ

void CLaunchItemDlg::OnLaunchitemIconref() 
{
	typedef BOOL (WINAPI *LPSHPICKICONDIALOG)(HWND, void*, DWORD, PDWORD);

	HINSTANCE hShell = ::LoadLibrary(_T("SHELL32.DLL"));
	if(hShell)
	{
		LPSHPICKICONDIALOG pfnSHPickIconDialog = (LPSHPICKICONDIALOG)::GetProcAddress(hShell, (LPCSTR)62);
		if( pfnSHPickIconDialog )
		{
			BOOL	bDeside;
			CString	strIconPathName;
			DWORD	dwIndex;
			if ( !IsUnicodeSystem() )
			{
				// 非NT系でアイコン選択ダイアログを表示
				char szIconFile[MAX_PATH] = "";
				bDeside = pfnSHPickIconDialog(m_hWnd, szIconFile, MAX_PATH, &dwIndex);
				strIconPathName = szIconFile;
			}
			else
			{
				// NT系でアイコン選択ダイアログを表示
				WCHAR szIconFile[MAX_PATH] = L"";
				bDeside = pfnSHPickIconDialog(m_hWnd, szIconFile, MAX_PATH, &dwIndex);
				if ( bDeside )
				{
					// Unicode文字列をShift-Jisに変換
					long	nChar = ::wcstombs( NULL, szIconFile, MAX_PATH );
					char*	pmbs = new char[ nChar+1 ];
					::wcstombs( pmbs, szIconFile, nChar );
					pmbs[nChar] = 0x00;
					strIconPathName = pmbs;
					delete [] pmbs;
				}
			}

			// アイコンが選択されたのなら反映する
			if ( bDeside )
			{
				m_idxIcon = dwIndex;
				m_strIconPathName = strIconPathName;
				RedrawWindow();
			}
		}
		::FreeLibrary(hShell);
	}
}

void CLaunchItemDlg::OnPaint() 
{
	CPaintDC dc(this); // 描画用のデバイス コンテキスト
	
	CRect	rcIcon;
	m_IconCanvas.GetWindowRect( &rcIcon );
	ScreenToClient( &rcIcon );

	// アイコンの描画
	::DrawIcon(
		&dc,
		rcIcon,
		m_strIconPathName,
		m_idxIcon
	);
}

void CLaunchItemDlg::OnOK() 
{
	UpdateData( TRUE );
	if ( m_pLaunchItem != NULL )
	{
		m_pLaunchItem->m_bEnable = m_bEnable;
		m_pLaunchItem->m_idxIcon = m_idxIcon;
		m_pLaunchItem->m_strButtonName = m_strName;
		m_pLaunchItem->m_strCommandLine = m_strCommand;
		m_pLaunchItem->m_strIconPathName = m_strIconPathName;
		m_pLaunchItem->m_bDefaultOpen = m_bDefaultOpen;
	}
	
	CDialog::OnOK();
}

BOOL CLaunchItemDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	if ( m_pLaunchItem != NULL )
	{
		m_bEnable = m_pLaunchItem->m_bEnable;
		m_idxIcon = m_pLaunchItem->m_idxIcon;
		m_strName = m_pLaunchItem->m_strButtonName;
		m_strCommand = m_pLaunchItem->m_strCommandLine;
		m_strIconPathName = m_pLaunchItem->m_strIconPathName;
		m_bDefaultOpen = m_pLaunchItem->m_bDefaultOpen;
	}
	UpdateData( FALSE );

	return TRUE;  // コントロールにフォーカスを設定しないとき、戻り値は TRUE となります
	              // 例外: OCX プロパティ ページの戻り値は FALSE となります
}

void CLaunchItemDlg::OnLaunchitemPath() 
{
	CString	strPath;
	strPath.Format(
		_T( "\"%s\\%s.%s\"" ),
		CLauncher::m_strKeywordDocPath,
		CLauncher::m_strKeywordDocName,
		CLauncher::m_strKeywordDocExt
	);
	m_editCommand.ReplaceSel(
		strPath
	);
	m_editCommand.SetFocus();
}

void CLaunchItemDlg::OnLaunchitemShitarabianfolder() 
{
	m_editCommand.ReplaceSel(
		CLauncher::m_strKeywordShitarabianFolder
	);
	m_editCommand.SetFocus();
}

void CLaunchItemDlg::OnLaunchitemShitarabianhandle() 
{
	m_editCommand.ReplaceSel(
		CLauncher::m_strKeywordWindowHandle
	);
	m_editCommand.SetFocus();
}

// ▼ (未実装機能削除) BGN 2004.06.09 (moralog)
/*
// ▽ original BGN
void CLaunchItemDlg::OnLaunchitemShitarabiancommand() 
{
	m_editCommand.ReplaceSel(
		CLauncher::m_strKeywordMessageID
	);
	m_editCommand.SetFocus();
}
// △ original END
*/
// ▲ (未実装機能削除) END

void CLaunchItemDlg::OnLaunchitemFileref() 
{
	// ファイル選択ダイアログを表示し、ダブルクォーテーションでくくる
	CFileDialog	dlgFile(
		TRUE,			// ← TRUEなら『開く』、FALSEなら、『名前を付けて保存』となる
		"*.*",			// ← デフォルトのファイル拡張子(NULLならなし)
		NULL,			// ← 初期表示されるファイル名(NULLならなし)
		OFN_HIDEREADONLY,
		"全てのファイル(*.*)|*.*|"	// ← ファイルの拡張子の種類
	);
	if ( dlgFile.DoModal() == IDOK )
	{
		CString		strFilePathName;
		strFilePathName = ::QuoteString( dlgFile.GetPathName() );
		m_editCommand.ReplaceSel(
			strFilePathName
		);
	}
	m_editCommand.SetFocus();
}

void CLaunchItemDlg::OnLaunchitemShitarabiancurentdoc() 
{
	m_editCommand.ReplaceSel(
		CLauncher::m_strKeywordShitarabianCurrentDoc
	);
	m_editCommand.SetFocus();
}

void CLaunchItemDlg::OnLaunchitemShitarabianselection() 
{
	m_editCommand.ReplaceSel(
		CLauncher::m_strKeywordShitarabianSelection
	);
	m_editCommand.SetFocus();
}
