/*===========================================================================
 *
 * CUSTOMLIST.CPP - 2 April 1999, Dave Humphrey (uesp@m0use.net)
 *
 * Custom CListBox class used for any of the character record data.  Includes
 * support for right-click pop-up menus and other things.
 *
 *=========================================================================*/

	/* Include Files */
#include "stdafx.h"
#include "CustomList.h"

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


/*===========================================================================
 *
 * Class CCustomList Constructor
 *
 * By default the list box has no popup menu and loses the currently 
 * selection when focus is lost.
 *
 *=========================================================================*/
CCustomList::CCustomList(void) {
  LoseSelect = TRUE;
 }
/*===========================================================================
 *		End of Class CCustomList Constructor
 *=========================================================================*/


/*===========================================================================
 *
 * Class CCustomList Message Map
 *
 *=========================================================================*/
BEGIN_MESSAGE_MAP(CCustomList, CListBox)
  //{{AFX_MSG_MAP(CCustomList)
  ON_WM_RBUTTONDOWN()
  ON_WM_KILLFOCUS()
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*===========================================================================
 *		End of Class CCustomList Message Map
 *=========================================================================*/


/*===========================================================================
 *
 * Class CCustomList Event - OnRButtonDown();
 *
 * Display the control's popup menu, if any.
 *
 *=========================================================================*/
void CCustomList::OnRButtonDown(UINT nFlags, CPoint point) {
  CMenu* pPopup;
  int    ItemHeight;
  int    NewItem;
  
	/* Call the base class member first */
  CListBox::OnRButtonDown(nFlags, point);

	/* Set focus to ourselves if required */
  SetFocus();

	/* Find which item was Right-Clicked */
  ItemHeight = GetItemHeight(0);
  NewItem = GetTopIndex() + point.y/ItemHeight;

	/* Change the current item if required */
  if (NewItem != GetCurSel() && NewItem >= 0 && NewItem < GetCount()) {
    SetCurSel(NewItem);
   }

	/* Modify the point coordinates to be screen absolute */
  ClientToScreen(&point);

	/* Load and display the list's pop-up menu, if any */
  pPopup = m_Menu.GetSubMenu(0);
  if (pPopup != NULL) pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
 }
/*===========================================================================
 *		End of Class CCustomList::OnRButtonDown();
 *=========================================================================*/


/*===========================================================================
 *
 * Class CCustomList Event - OnKillFocus();
 *
 * Resets the currently selected item, if so desired by the user.
 *
 *=========================================================================*/
void CCustomList::OnKillFocus(CWnd* pNewWnd) {

	/* Call the base class member first */
  CListBox::OnKillFocus(pNewWnd);

	/* Only reset selection if required */
  if (LoseSelect)  SetCurSel(-1);	
 }
/*===========================================================================
 *		End of Class Event CCustomList::OnKillFocus()
 *=========================================================================*/