// ★コメント未記入
// PlugInWallDlg.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "shitarabian.h"
#include "PlugInWallDlg.h"
#include "HiddenDecoder.h"
#include "Common.h"
#include "ShitarabianPlugInWall.h"
#include "ExeCommon.h"
#include "SettingPath.h"

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


	BOOL	mbImageExists = FALSE;
	HBITMAP	mhBitmap;

/////////////////////////////////////////////////////////////////////////////
// CPlugInWallDlg ダイアログ


CPlugInWallDlg::CPlugInWallDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlugInWallDlg::IDD, pParent)
	, m_pDoc( NULL )
	, m_pParent( NULL )

{
	//{{AFX_DATA_INIT(CPlugInWallDlg)
	m_bWallEnable = FALSE;
	m_iOffsetX = 0;
	m_iOffsetY = 0;
	m_uiSizeX = 0;
	m_uiSizeY = 0;
	m_bResize = FALSE;
	m_strImageFileName = _T("");
	//}}AFX_DATA_INIT
}


void CPlugInWallDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlugInWallDlg)
	DDX_Control(pDX, IDC_WALL_SIZEY, m_editSizeY);
	DDX_Control(pDX, IDC_WALL_SIZEX, m_editSizeX);
	DDX_Control(pDX, IDC_WALL_STATICEFFECT5, m_staticEffect5);
	DDX_Control(pDX, IDC_WALL_RGB, m_sliderRGB);
	DDX_Control(pDX, IDC_WALL_COLOR_INIT, m_btnColorInit);
	DDX_Control(pDX, IDC_WALL_STATICEFFECT4, m_staticEffect4);
	DDX_Control(pDX, IDC_WALL_STATICEFFECT3, m_staticEffect3);
	DDX_Control(pDX, IDC_WALL_STATICEFFECT2, m_staticEffect2);
	DDX_Control(pDX, IDC_WALL_STATICEFFECT1, m_staticEffect1);
	DDX_Control(pDX, IDC_WALL_RED, m_sliderRed);
	DDX_Control(pDX, IDC_WALL_GREEN, m_sliderGreen);
	DDX_Control(pDX, IDC_WALL_BLUE, m_sliderBlue);
	DDX_Control(pDX, IDC_WALL_GRAY, m_sliderGray);
	DDX_Check(pDX, IDC_WALL_ENABLE, m_bWallEnable);
	DDX_Text(pDX, IDC_WALL_OFFSETX, m_iOffsetX);
	DDX_Text(pDX, IDC_WALL_OFFSETY, m_iOffsetY);
	DDX_Text(pDX, IDC_WALL_SIZEX, m_uiSizeX);
	DDX_Text(pDX, IDC_WALL_SIZEY, m_uiSizeY);
	DDX_Check(pDX, IDC_WALL_RESIZE, m_bResize);
	DDX_Text(pDX, IDC_WALL_IMAGEFILENAME, m_strImageFileName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPlugInWallDlg, CDialog)
	//{{AFX_MSG_MAP(CPlugInWallDlg)
	ON_BN_CLICKED(IDC_WALL_REF, OnWallRef)
	ON_BN_CLICKED(IDC_WALL_COLOR_INIT, OnWallColorInit)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_WALL_RGB, OnReleasedcaptureWallRgb)
	ON_BN_CLICKED(IDC_WALL_RESIZE, OnWallResize)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

void CPlugInWallDlg::OnWallRef() 
{
	UpdateData( TRUE );

	// 画像ファイル名の入力
	{
		// サポート拡張子の取得
		CString		strSupportedExt = "BMP|*.bmp|";
		CString		strDefaultExt;
		{
			CHiddenDecoder	Decoder;
			Decoder.PluginFolder(
				CSettingPath::GetInstance()->GetFolderPath()
			);
			CStringArray		aExtz;
			Decoder.GetSupportExt(
				&aExtz
			);
			for( int idxExt=0 ; idxExt<aExtz.GetSize() ; idxExt++ )
			{
				strSupportedExt += aExtz[idxExt];
				strSupportedExt += _T("|");
			}
			strSupportedExt += _T("|");

			if ( aExtz.GetSize() >= 2 )
			{
				strDefaultExt = aExtz[1];
			}
		}

		CFileDialog		dlgFile(
			TRUE,			// ← TRUEなら『開く』、FALSEなら、『名前を付けて保存』となる
			strDefaultExt,	// ← デフォルトのファイル拡張子(NULLならなし)
			NULL,			// ← 初期表示されるファイル名(NULLならなし)
			OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,
			strSupportedExt	// ← ファイルの拡張子の種類
		);

		int		iResult;
		iResult = dlgFile.DoModal();
		switch( iResult )
		{
		case IDOK:
			m_strImageFileName = dlgFile.GetPathName();

			if( m_pParent != NULL )
			if( m_pDoc != NULL )
			{
				m_pParent->ReflectFromDialog( m_pDoc );
			}

			UpdateData( FALSE );
			break;

		case IDCANCEL:
		default:
			break;
		}
	}	
}

BOOL CPlugInWallDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_sliderGray.SetRange( 0, 255 );
	m_sliderRed.SetRange( 0, 255+255 );
	m_sliderGreen.SetRange( 0, 255+255 );
	m_sliderBlue.SetRange( 0, 255+255 );
	m_sliderRGB.SetRange( 0, 255+255 );

	m_sliderGray.SetPos( m_iGray );
	m_sliderRed.SetPos( m_uiRed+255 );
	m_sliderGreen.SetPos( m_uiGreen+255 );
	m_sliderBlue.SetPos( m_uiBlue+255 );
	m_sliderRGB.SetPos( m_uiBlue+255 );

	m_sliderGray.EnableWindow( m_bEffect );
	m_sliderRed.EnableWindow( m_bEffect );
	m_sliderGreen.EnableWindow( m_bEffect );
	m_sliderBlue.EnableWindow( m_bEffect );
	m_sliderRGB.EnableWindow( m_bEffect );
	m_btnColorInit.EnableWindow( m_bEffect );
	m_staticEffect1.EnableWindow( m_bEffect );
	m_staticEffect2.EnableWindow( m_bEffect );
	m_staticEffect3.EnableWindow( m_bEffect );
	m_staticEffect4.EnableWindow( m_bEffect );
	m_staticEffect5.EnableWindow( m_bEffect );

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

void CPlugInWallDlg::OnOK() 
{
	m_iGray = m_sliderGray.GetPos();
	m_uiRed = m_sliderRed.GetPos() -255;
	m_uiGreen = m_sliderGreen.GetPos() -255;
	m_uiBlue = m_sliderBlue.GetPos() -255;	

	CDialog::OnOK();
}

void CPlugInWallDlg::OnWallColorInit() 
{
	m_sliderGray.SetPos( 0 );
	m_sliderRed.SetPos( 255 );
	m_sliderGreen.SetPos( 255 );
	m_sliderBlue.SetPos( 255 );
	m_sliderRGB.SetPos( 255 );
}

void CPlugInWallDlg::OnReleasedcaptureWallRgb(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// warning C4100:警告の無視
	pNMHDR;

	int		iRgb = m_sliderRGB.GetPos();
	m_sliderRed.SetPos( iRgb );
	m_sliderGreen.SetPos( iRgb );
	m_sliderBlue.SetPos( iRgb );

	*pResult = 0;
}

void CPlugInWallDlg::OnWallResize() 
{
	VisibleContorol();
}

void CPlugInWallDlg::VisibleContorol()
{
	UpdateData();

	m_editSizeX.EnableWindow( m_bResize );
	m_editSizeY.EnableWindow( m_bResize );
}
