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

#include "stdafx.h"
#include "shitarabian.h"
#include "LaunchListCtrl.h"
#include "LaunchDlg.h"
#include "ExeCommon.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLaunchListCtrl

CLaunchListCtrl::CLaunchListCtrl()
: m_pLaunchDefine( NULL )
{
}

CLaunchListCtrl::~CLaunchListCtrl()
{
}


BEGIN_MESSAGE_MAP(CLaunchListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CLaunchListCtrl)
	ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

void CLaunchListCtrl::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

	COLORREF	colBack = RGB(255,255,255);
	COLORREF	colFore = RGB(0,0,0);

	BOOL	bSelected = ( lpDrawItemStruct->itemState & ODS_SELECTED );
	if ( bSelected )
	{
		colBack = RGB(64,128,128);
		colFore = RGB(255,255,255);
	}

	pDC->FillSolidRect(
		&(lpDrawItemStruct->rcItem),
		colBack
	);

	int	iLeft = lpDrawItemStruct->rcItem.left;
	pDC->SetTextColor( colFore );

	CArray<CLaunchItem, CLaunchItem&>& Itemz = *(m_pLaunchDefine->GetLaunchDefine());
	CLaunchItem&	Launch = Itemz[lpDrawItemStruct->itemID];

	// 有効
	{
		CRect rcClient(
			iLeft+2,
			lpDrawItemStruct->rcItem.top,
			iLeft+GetColumnWidth(0),
			lpDrawItemStruct->rcItem.bottom
		);
		if( Launch.m_bEnable )
		{
			pDC->DrawText(
				"■",
				&rcClient,
				DT_CENTER
			);
		}
		else
		{
			pDC->DrawText(
				"□",
				&rcClient,
				DT_CENTER
			);
		}
	}
	iLeft += GetColumnWidth(0);

	// アイコン
	{
		CRect rcClient(
			iLeft+2,
			lpDrawItemStruct->rcItem.top,
			iLeft+GetColumnWidth(1),
			lpDrawItemStruct->rcItem.bottom
		);

		CRgn	rgn;
		rgn.CreateRectRgn(
			rcClient.left,
			rcClient.top,
			rcClient.right,
			rcClient.bottom
		);
		pDC->SelectClipRgn( NULL );
		pDC->SelectClipRgn(
			&rgn,
			RGN_AND
		);

		// アイコンの描画
		::DrawIcon(
			pDC,
			rcClient,
			Launch.m_strIconPathName,
			Launch.m_idxIcon
		);

		pDC->SelectClipRgn( NULL );
		rgn.DeleteObject();
	}
	iLeft += GetColumnWidth(1);

	// 名称
	{
		CRect rcClient(
			iLeft+2,
			lpDrawItemStruct->rcItem.top,
			iLeft+GetColumnWidth(2),
			lpDrawItemStruct->rcItem.bottom
		);

		CRgn	rgn;
		rgn.CreateRectRgn(
			rcClient.left,
			rcClient.top,
			rcClient.right,
			rcClient.bottom
		);
		pDC->SelectClipRgn( NULL );
		pDC->SelectClipRgn(
			&rgn,
			RGN_AND
		);

		{
			pDC->DrawText(
				Launch.m_strButtonName,
				rcClient,
				DT_LEFT
			);
		}
		pDC->SelectClipRgn( NULL );
		rgn.DeleteObject();
	}
	iLeft += GetColumnWidth(2);

	// コマンド
	{
		CRect rcClient(
			iLeft+2,
			lpDrawItemStruct->rcItem.top,
			iLeft+GetColumnWidth(3),
			lpDrawItemStruct->rcItem.bottom
		);

		CRgn	rgn;
		rgn.CreateRectRgn(
			rcClient.left,
			rcClient.top,
			rcClient.right,
			rcClient.bottom
		);
		pDC->SelectClipRgn( NULL );
		pDC->SelectClipRgn(
			&rgn,
			RGN_AND
		);

		{
			pDC->DrawText(
				Launch.m_strCommandLine,
				rcClient,
				DT_LEFT|DT_NOPREFIX
			);
		}
		pDC->SelectClipRgn( NULL );
		rgn.DeleteObject();
	}
	iLeft += GetColumnWidth(3);

	// 枠線
	CPen	pen;
	pen.CreatePen( PS_SOLID, 1, RGB(200,200,200 ) );
	CPen*	ppenOld = (CPen*)pDC->SelectObject( &pen );
	pDC->MoveTo( lpDrawItemStruct->rcItem.left, lpDrawItemStruct->rcItem.bottom-1 );
	pDC->LineTo( iLeft-1, lpDrawItemStruct->rcItem.bottom-1 );

// ▼ (不具合処置) BGN 2004.06.09 (moralog) 横スクロールした状態だと枠線がずれる
// ▽ original BGN
//	iLeft = 0;
// △ original END
	iLeft = lpDrawItemStruct->rcItem.left;
// ▲ (不具合処置) END
	for( int idxColumn=0 ; idxColumn<4 ; idxColumn++ )
	{
		iLeft += GetColumnWidth( idxColumn );
		pDC->MoveTo( iLeft-1, lpDrawItemStruct->rcItem.top );
		pDC->LineTo( iLeft-1, lpDrawItemStruct->rcItem.bottom-1 );
	}

	pDC->SelectObject( ppenOld );
}

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

	// チェック部分をクリックした場合、有効無効を切り替える
	CPoint	ptCursor;
	::GetCursorPos( &ptCursor );
	ScreenToClient( &ptCursor );
	int	idxItem = HitTest( ptCursor );
	if ( idxItem != -1 )
	{
		// チェック部分のクリック
		if ( ptCursor.x < GetColumnWidth(0) )
		{
			// 有効無効をトグルする
			CArray<CLaunchItem, CLaunchItem&>& Itemz = *( m_pLaunchDefine->GetLaunchDefine() );
			Itemz[idxItem].m_bEnable = !Itemz[idxItem].m_bEnable;

			Update( idxItem );
		}
	}

	((CLaunchDlg*)GetParent())->VisibleControl();

	*pResult = 0;
}
