#include "stdafx.h"
#include "SkinListCtrl.h"

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

CSkinListCtrl::CSkinListCtrl()
{
	g_MyClrBgHi = RGB(115,123,165);
	g_MyClrFgHi = RGB(229,229,229);
}

CSkinListCtrl::~CSkinListCtrl()
{
}


BEGIN_MESSAGE_MAP(CSkinListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CSkinListCtrl)
	ON_WM_NCCALCSIZE()
	ON_WM_MOUSEWHEEL()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_DROPFILES()
	//}}AFX_MSG_MAP
	ON_NOTIFY_REFLECT ( NM_CUSTOMDRAW, OnCustomDrawList )
END_MESSAGE_MAP()

BOOL CSkinListCtrl::SetBkImage(UINT nIDResource)
{
	return SetBkImage((LPCTSTR)nIDResource);
}

BOOL CSkinListCtrl::SetBkImage(LPCTSTR lpszResourceName)
{
	// If this is not the first call then Delete GDI objects
	if( m_bitmap.m_hObject != NULL )		m_bitmap.DeleteObject();
	if( m_pal.m_hObject != NULL )		m_pal.DeleteObject();
	
	
	HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(), lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION );

	if( hBmp == NULL ) 		return FALSE;

	m_bitmap.Attach( hBmp );
	BITMAP bm;
	m_bitmap.GetBitmap( &bm );
	m_cxBitmap = bm.bmWidth;
	m_cyBitmap = bm.bmHeight;

	// Create a logical palette for the bitmap
	DIBSECTION ds;
	BITMAPINFOHEADER &bmInfo = ds.dsBmih;
	m_bitmap.GetObject( sizeof(ds), &ds );

	int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;

	// Create a halftone palette if colors > 256. 
	CClientDC dc(NULL);			// Desktop DC
	if( nColors > 256 )
		m_pal.CreateHalftonePalette( &dc );
	else
	{
		// Create the palette

		RGBQUAD *pRGB = new RGBQUAD[nColors];
		CDC memDC;
		memDC.CreateCompatibleDC(&dc);

		memDC.SelectObject( &m_bitmap );
		::GetDIBColorTable( memDC, 0, nColors, pRGB );

		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];

		pLP->palVersion = 0x300;
		pLP->palNumEntries = nColors;

		for( int i=0; i < nColors; i++)
		{
			pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
			pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
			pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
			pLP->palPalEntry[i].peFlags = 0;
		}

		m_pal.CreatePalette( pLP );

		delete[] pLP;
		delete[] pRGB;
	}
	Invalidate();

	return TRUE;
}
void CSkinListCtrl::PreSubclassWindow() 
{

	m_SkinHeaderCtrl.SubclassWindow(GetHeaderCtrl()->m_hWnd);
	CListCtrl::PreSubclassWindow();
}


void CSkinListCtrl::OnCustomDrawList ( NMHDR* pNMHDR, LRESULT* pResult )
{
	NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
	static bool bHighlighted = false;
	
    *pResult = CDRF_DODEFAULT;
	
    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
	{
        *pResult = CDRF_NOTIFYITEMDRAW;
	}
    else 
		if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
		{
			int iRow = (int)pLVCD->nmcd.dwItemSpec;
			
			bHighlighted = IsRowHighlighted(m_hWnd, iRow);
			if (bHighlighted)
			{
				pLVCD->clrText   = g_MyClrFgHi; // Use my foreground hilite color
				pLVCD->clrTextBk = g_MyClrBgHi; // Use my background hilite color
				EnableHighlighting(m_hWnd, iRow, false);
			}
			
			*pResult = CDRF_DODEFAULT | CDRF_NOTIFYPOSTPAINT;
			
		}
		else if(CDDS_ITEMPOSTPAINT == pLVCD->nmcd.dwDrawStage)
		{
			if (bHighlighted)
			{
				int  iRow = (int)pLVCD->nmcd.dwItemSpec;
				
				EnableHighlighting(m_hWnd, iRow, true);
			}
			
			*pResult = CDRF_DODEFAULT;
			
		}
}

void CSkinListCtrl::EnableHighlighting(HWND hWnd, int row, bool bHighlight)
{
  ListView_SetItemState(hWnd, row, bHighlight? 0xff: 0, LVIS_SELECTED);
}

bool CSkinListCtrl::IsRowSelected(HWND hWnd, int row)
{
  return ListView_GetItemState(hWnd, row, LVIS_SELECTED) != 0;
}

bool CSkinListCtrl::IsRowHighlighted(HWND hWnd, int row)
{

  return IsRowSelected(hWnd, row) ;
}

void CSkinListCtrl::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp) 
{
	UpdateWindow();
	CListCtrl::OnNcCalcSize(bCalcValidRects, lpncsp);
}

BOOL CSkinListCtrl::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
{

	return CListCtrl::OnMouseWheel(nFlags, zDelta, pt);
}


void CSkinListCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{

	CListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CSkinListCtrl::Init()
{
	//another way to hide scrollbars
	InitializeFlatSB(this->m_hWnd);
	FlatSB_EnableScrollBar(this->m_hWnd, SB_BOTH, ESB_DISABLE_BOTH);


	CWnd* pParent = GetParent();

	CRect windowRect;
	GetWindowRect(&windowRect);

	int nTitleBarHeight = 0;

	if(pParent->GetStyle() & WS_CAPTION)
		nTitleBarHeight = GetSystemMetrics(SM_CYSIZE);
	

	int nDialogFrameHeight = 0;
	int nDialogFrameWidth = 0;
	if((pParent->GetStyle() & WS_BORDER))
	{
		nDialogFrameHeight = GetSystemMetrics(SM_CYDLGFRAME);
		nDialogFrameWidth = GetSystemMetrics(SM_CYDLGFRAME);
	}

	if(pParent->GetStyle() & WS_THICKFRAME)
	{
		nDialogFrameHeight+=1;
		nDialogFrameWidth+=1;
	}

}

void CSkinListCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	CListCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
}

BOOL CSkinListCtrl::OnEraseBkgnd(CDC* pDC) 
{
//	if( m_bitmap.m_hObject != NULL )	
	//	return TRUE;
	
	//return CListCtrl::OnEraseBkgnd(pDC);
	
	//CDC *pDC=CDC::FromHandle(hDC);
	CDC memDC;

	CBitmap pp;
	pp.LoadBitmap(IDB_LISTBK);
	CBitmap* pBitmap;
	pBitmap=&pp;
	CBitmap* pOld=NULL;
	memDC.CreateCompatibleDC(pDC);
	BITMAP	bmpInfo;
	
	ASSERT(pBitmap->m_hObject!=NULL);
	pBitmap->GetBitmap(&bmpInfo);
	
	pOld=memDC.SelectObject((CBitmap*) pBitmap);
	if (pOld==NULL) return FALSE; //Destructors will clean up

	pDC->StretchBlt(0,15,496+13,360,&memDC,0,0,496,360,SRCCOPY);//part1
	
	memDC.SelectObject(pOld);
	//MessageBox("skinlist OnEraseBkgnd ","kk",MB_OK);
	return TRUE;

}


void CSkinListCtrl::OnPaint() 
{
	//

	//
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(&rect);

	CMemDC memDC(&dc, rect);
	
	CRect headerRect;
	GetDlgItem(0)->GetWindowRect(&headerRect);
	ScreenToClient(&headerRect);
	dc.ExcludeClipRect(&headerRect);
	   
	   
	CRect clip;
	memDC.GetClipBox(&clip);
	this->GetClientRect(&clip);
	memDC.FillSolidRect(clip, RGB(239,235,239));
	////
	//CDC *pDC=CDC::FromHandle(hDC);
		//CDC memDC;
	/*
			CDC mdc;
				mdc.CreateCompatibleDC(NULL);
		
				CBitmap pp;
				pp.LoadBitmap(IDB_LISTBK);
				CBitmap* pBitmap;
				pBitmap=&pp;
				CBitmap* pOld=NULL;
				//memDC.CreateCompatibleDC(dc);
				BITMAP	bmpInfo;
				
				ASSERT(pBitmap->m_hObject!=NULL);
				pBitmap->GetBitmap(&bmpInfo);
				
				pOld=memDC.SelectObject((CBitmap*) pBitmap);	
				if (pOld==NULL) return; //Destructors will clean up*/
		

	//	pDC->StretchBlt(271,0,233,44,&memDC,0,0,233,44,SRCCOPY);//part1
		//memDC->m_pDC->StretchBlt(0,0,496,360,&mdc,0,0,496,360,SRCCOPY);
		//dc.StretchBlt(0,0,496+10,360+10,&mdc,0,0,496+10,360+10,SRCCOPY);
	//	memDC.SelectObject(pOld);
	/////
	//memDC.FillSolidRect(clip, RGB(239,235,0));
	   
	SetTextBkColor(RGB(239,235,239));
	  
	DefWindowProc(WM_PAINT, (WPARAM)memDC->m_hDC, (LPARAM)0);
}
