// ListBoxPro.cpp : implementation file
//

#include "stdafx.h"
#include "imagegrinder.h"
#include "ListBoxPro.h"



/////////////////////////////////////////////////////////////////////////////
// CListBoxPro

CListBoxPro::CListBoxPro()
{
Reordered=false;
Selection=0;

}

CListBoxPro::~CListBoxPro()
{
}


BEGIN_MESSAGE_MAP(CListBoxPro, CListBox)
//{{AFX_MSG_MAP(CListBoxPro)
ON_WM_RBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListBoxPro message handlers

void CListBoxPro::OnRButtonDown(UINT nFlags, CPoint point) 
{
	RCPos=-1;
	NMHDR nmhdr;
	nmhdr.hwndFrom=m_hWnd;
	nmhdr.idFrom=GetDlgCtrlID();
	nmhdr.code=NM_RCLICK;
	
	UINT item;
	BOOL outside;
	item=ItemFromPoint(point,outside);
	if(!outside)
	{
	RCPos=item;
	}
	
		GetParent()->SendMessage(WM_NOTIFY,GetDlgCtrlID(),(DWORD)&nmhdr);
	CListBox::OnRButtonDown(nFlags, point);
}

void CListBoxPro::OnLButtonUp(UINT nFlags, CPoint point) 
{
	CListBox::OnLButtonUp(nFlags, point);
}

void CListBoxPro::OnLButtonDown(UINT nFlags, CPoint point) 
{
	NMHDR nmhdr;
	nmhdr.hwndFrom=m_hWnd;
	nmhdr.idFrom=GetDlgCtrlID();
	nmhdr.code=NM_CLICK;


	Reordered=false;
	CListBox::OnLButtonDown(nFlags, point);
	OldPos=Selection=GetCurSel();	
	GetParent()->SendMessage(WM_NOTIFY,GetDlgCtrlID(),(DWORD)&nmhdr);
}

void CListBoxPro::OnMouseMove(UINT nFlags, CPoint point) 
{
	CListBox::OnMouseMove(nFlags, point);
	if(nFlags&MK_LBUTTON)
	{
		int newSel;
		newSel=GetCurSel();	
		if(newSel!=Selection)
		{
			DWORD dat=GetItemData(Selection);
			void *dpt=GetItemDataPtr(Selection);
			CString str;
			GetText(Selection,str);
			
			DeleteString(Selection);
			int pos=InsertString(newSel,str);
			SetItemDataPtr(pos,dpt);
			SetItemData(pos,dat);
			
			NewPos=Selection=newSel;
			SetCurSel(Selection);
			Reordered=true;
		}
	}	
}

BOOL CListBoxPro::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

void CListBoxPro::OnLButtonDblClk(UINT nFlags, CPoint point) 
{

	NMHDR nmhdr;
	nmhdr.hwndFrom=m_hWnd;
	nmhdr.idFrom=GetDlgCtrlID();
	nmhdr.code=NM_DBLCLK;

	GetParent()->SendMessage(WM_NOTIFY,GetDlgCtrlID(),(DWORD)&nmhdr);
	
//	CListBox::OnLButtonDblClk(nFlags, point);
}
