#include "stdafx.h"
#include "../Ctrl/Button.h"
#include "../Ctrl/EditBox.h"
#include "../Ctrl/Label.h"
#include "../Option.h"
#include "FolderDialog.h"
#include "FolderInputDialog.h"

INT_PTR CFolderInputDialog::DoModal(HWND hWnd, LPTSTR pSaveDir)
{
	m_pSaveDir = pSaveDir;
	HINSTANCE hInst = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
	INT_PTR ret = DialogBoxParam(hInst, _T("FOLDER_INPUT_DLG"), hWnd, (DLGPROC)WndStaticProc, (LPARAM)this);
	return (ret);
}

LRESULT CFolderInputDialog::WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
	static CButton BtnDir, BtnOk, BtnCancel;
	static CEditBox EditDir;
	static CLabel LabelDir;

	switch (msg) {
		case WM_INITDIALOG:
		{
			// D&Dを許可
			DragAcceptFiles(hWnd, TRUE);

			UINT ID = 10000;
			int x = 10, y = -10;

			SetWindowText(hWnd, _T("出力先の選択"));

			LabelDir.Create(hWnd, _T("フォルダ名を入力"), ID++, x, y += 20, 100, 20);
			EditDir.Create(hWnd, m_pSaveDir, ID++, x, y += 20, 300, 22);
			BtnDir.Create(hWnd, _T("参照"), ID++, x + 300, y + 1, 40, 20);

			BtnOk.Create(hWnd, _T("OK"), IDOK, 110, y += 30, 70, 23);
			BtnCancel.Create(hWnd, _T("キャンセル"), IDCANCEL, 190, y, 70, 23);

			EditDir.SetFocus();
			BtnOk.SetDef();

			Init(380, y + 60);

			return FALSE;
		}

		case WM_DROPFILES:
		{
			TCHAR szSaveDir[_MAX_DIR];
			HDROP hDrop = (HDROP)wp;
			DragQueryFile(hDrop, 0, szSaveDir, sizeof(szSaveDir));
			EditDir.SetText(szSaveDir);
			return FALSE;
		}

		case WM_COMMAND:
			// 参照ボタンが押された
			if (LOWORD(wp) == BtnDir.GetID()) {
				TCHAR szSaveDir[_MAX_DIR];
				lstrcpy(szSaveDir, m_pSaveDir);
				CFolderDialog FolderDlg;
				if (FolderDlg.DoModal(hWnd, _T("フォルダ選択"), szSaveDir) == TRUE)
					EditDir.SetText(szSaveDir);
				return FALSE;
			}
			// OKボタンが押された
			if (LOWORD(wp) == IDOK) {
				EditDir.GetText(m_pSaveDir, _MAX_DIR);
				PathRemoveBackslash(m_pSaveDir);
				EndDialog(hWnd, IDOK);
				return TRUE;
			}
			// キャンセルボタンが押された
			if (LOWORD(wp) == IDCANCEL) {
				EndDialog(hWnd, IDCANCEL);
				return TRUE;
			}
			return FALSE;
	}

	return FALSE;
}