// ★コメント未記入
// EmphasisListCtrl.cpp : インプリメンテーション ファイル
//
//		2004-05-10	b040111 (3344) 共通設定のユニコード文字で追加のボタンを押すと強制終了 (moralog)


#include "stdafx.h"
#include "shitarabian.h"
#include "EmphasisListCtrl.h"
#include "EditDrawer.h"
#include "EmphasisDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEmphasisListCtrl

CEmphasisListCtrl::CEmphasisListCtrl()
: m_pEmphasisDefine( NULL )
{
}

CEmphasisListCtrl::~CEmphasisListCtrl()
{
}


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

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

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

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

//	BOOL	bSelected = ( lpDrawItemStruct->itemID == GetSelectionMark() );
	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<CEmphasisItem, CEmphasisItem&>& Itemz = m_pEmphasisDefine->GetItemz();
	CEmphasisItem&	Emphasis = Itemz[lpDrawItemStruct->itemID];
	// 有効
	if( Emphasis.GetEnableToggle() )
	{
		CRect rcClient(
			iLeft+2,
			lpDrawItemStruct->rcItem.top,
			iLeft+GetColumnWidth(0),
			lpDrawItemStruct->rcItem.bottom
		);
		if( Emphasis.GetCheck() )
		{
			pDC->DrawText(
				"■",
				&rcClient,
				DT_CENTER
			);
		}
		else
		{
			pDC->DrawText(
				"□",
				&rcClient,
				DT_CENTER
			);
		}
	}
	iLeft += GetColumnWidth(0);

	// パターン
	{
		CEmphasisItem&	Emphasis = Itemz[lpDrawItemStruct->itemID];

		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
		);

		CString	strPattern;
		strPattern = Emphasis.GetPattern();
		if ( !Emphasis.GetTerminator().IsEmpty() )
		{
			strPattern += "〜" + Emphasis.GetTerminator();
		}

		if( bSelected )
		{
			CEditDrawer::GetInstance()->ReflectFont( pDC );
			pDC->DrawText(
				strPattern,
				rcClient,
// ▼ (3344) BGN 2004.05.27 (moralog) 表示時に&が消える不具合処置
// ▽ original BGN
//				DT_LEFT
// △ original END
				DT_LEFT | DT_NOPREFIX
// ▲ (3344) END
			);
			CEditDrawer::GetInstance()->RestoreFont( pDC );
		}
		else
		{
			CEditDrawer::GetInstance()->SetDstX( rcClient.left );
			CEditDrawer::GetInstance()->SetDstY( rcClient.top );
			CEditDrawer::GetInstance()->DrawString(
				strPattern,
				pDC,
				m_pEmphasisDefine
			);
		}
		pDC->SelectClipRgn( NULL );
		rgn.DeleteObject();
	}
	iLeft += GetColumnWidth(1);

	// 枠線
	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 );

	iLeft = 0;
	for( int idxColumn=0 ; idxColumn<2 ; idxColumn++ )
	{
		iLeft += GetColumnWidth( idxColumn );
		pDC->MoveTo( iLeft-1, lpDrawItemStruct->rcItem.top );
		pDC->LineTo( iLeft-1, lpDrawItemStruct->rcItem.bottom-1 );
	}

	pDC->SelectObject( ppenOld );
}

void CEmphasisListCtrl::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<CEmphasisItem, CEmphasisItem&>& Itemz = m_pEmphasisDefine->GetItemz();
			Itemz[idxItem].SetEnable( !Itemz[idxItem].GetCheck() );

			Update( idxItem );
		}
	}

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

	*pResult = 0;
}
