// MyListCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "MyListCtrl.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyListCtrl

CMyListCtrl::CMyListCtrl()
{
}

CMyListCtrl::~CMyListCtrl()
{
}


BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CMyListCtrl)
	ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyListCtrl message handlers

void CMyListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// Get the current mouse location and convert it to client
	// coordinates.
	DWORD pos = GetMessagePos();

	CPoint pt(LOWORD(pos), HIWORD(pos));
	ScreenToClient(&pt);

	// Get indexes of the first and last visible items in listview
	// control.
	int index = GetTopIndex();

	int last_visible_index = index + GetCountPerPage();

	if (last_visible_index > GetItemCount())
		last_visible_index = GetItemCount();

	// Loop until number visible items has been reached.

	while (index <= last_visible_index)
	{

		// Get the bounding rectangle of an item. If the mouse

		// location is within the bounding rectangle of the item,

		// you know you have found the item that was being clicked.

		CRect r;
		GetItemRect(index, &r, LVIR_BOUNDS);


		if (r.PtInRect(pt))
		{
			UINT flag = LVIS_SELECTED | LVIS_FOCUSED;

			SetItemState(index, flag, flag);
			break;

		}

		// Get the next item in listview control.
		index++;
	}

	
	*pResult = 0;
}

void CMyListCtrl::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	
	// Get the current mouse location and convert it to client
	// coordinates.
	DWORD pos = GetMessagePos();

	CPoint pt(LOWORD(pos), HIWORD(pos));
	ScreenToClient(&pt);

	// Get indexes of the first and last visible items in listview
	// control.
	int index = GetTopIndex();

	int last_visible_index = index + GetCountPerPage();

	if (last_visible_index > GetItemCount())
		last_visible_index = GetItemCount();

	// Loop until number visible items has been reached.

	while (index <= last_visible_index)
	{

		// Get the bounding rectangle of an item. If the mouse

		// location is within the bounding rectangle of the item,

		// you know you have found the item that was being clicked.

		CRect r;
		GetItemRect(index, &r, LVIR_BOUNDS);


		if (r.PtInRect(pt))
		{
			
			//Select item
			UINT flag = LVIS_SELECTED | LVIS_FOCUSED;

			SetItemState(index, flag, flag);


			//Show PopUp Menu
			CMenu menu;
			VERIFY(menu.LoadMenu(IDR_MENU1));
			CMenu* pPopup = menu.GetSubMenu(0);
			
			//Modify the menu with the ressource name. The Command is handled in CPropertySheet::OnCmdMsg.
			//pPopup->ModifyMenu( 0, MF_BYPOSITION | MF_STRING, ID_WATCH, "Voila, ca marche!!" );
			
			ASSERT(pPopup != NULL); 
			pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
				LOWORD(pos),
				HIWORD(pos),
				AfxGetMainWnd());

			

			break;

		}

		// Get the next item in listview control.
		index++;
	}

	*pResult = 0;
}
