// ObjManListCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "ObjManListCtrl.h"



/////////////////////////////////////////////////////////////////////////////
// CObjManListCtrl

CObjManListCtrl::CObjManListCtrl()
{
	SortCol=0;
}

CObjManListCtrl::~CObjManListCtrl()
{
}


BEGIN_MESSAGE_MAP(CObjManListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CObjManListCtrl)
	ON_WM_CHAR()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



LRESULT CObjManListCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	switch(message)
	{
	case WM_CHAR:
	case WM_KEYDOWN:
	case WM_KEYUP:
		GetParent()->SendMessage(message,wParam,lParam);
	}
	
	return CListCtrl::WindowProc(message, wParam, lParam);
}

int CALLBACK 
CompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
   CObjManListCtrl* pListCtrl = (CObjManListCtrl*) lParamSort;
   CString    strItem1,strItem2;
	int itm1,itm2;
	itm1=pListCtrl->GetItemByData(lParam1);
	itm2=pListCtrl->GetItemByData(lParam2);
	strItem1 = pListCtrl->GetItemText(itm1, pListCtrl->SortCol);
	strItem2 = pListCtrl->GetItemText(itm2,  pListCtrl->SortCol);

   int res;
   res=strItem1.CompareNoCase(strItem2);
   if(res==0)
   {
   int ccount=0;
   LVCOLUMN lv;
   lv.mask=LVCF_SUBITEM;
   for(ccount=0;pListCtrl->GetColumn(ccount,&lv);ccount++)
   { 
   if(res==0 && pListCtrl->SortCol!=ccount)
		{
		  strItem1 = pListCtrl->GetItemText(itm1, ccount);
		  strItem2 = pListCtrl->GetItemText(itm2, ccount);
			res=strItem1.CompareNoCase(strItem2);
		}
	}
   }
   return res;
}

void CObjManListCtrl::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	SortCol=pNMListView->iSubItem;
	SortItems(CompareProc,(DWORD)this);

	*pResult = 0;
}

int CObjManListCtrl::GetItemByData(DWORD data)
{
int i;
for(i=0;i<GetItemCount();i++)
{
if(data==GetItemData(i)) return i;
}

return -1;
}

void CObjManListCtrl::DeleteAllColumns()
{
int nColumnCount = GetHeaderCtrl()->GetItemCount();
for (int i=0;i < nColumnCount;i++)  DeleteColumn(0);
}

int CObjManListCtrl::GetColWithID(int sub)
{
for(int i=0;i<COLMAX;i++) 
	if(ColID[i]==sub) 
		return i;
return -1;
}
