home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / addins / wins / mylistctrl.cpp < prev    next >
C/C++ Source or Header  |  1998-04-08  |  5KB  |  236 lines

  1. // MyListCtrl.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "resource.h"
  6. #include "MyListCtrl.h"
  7. #include "fileitem.h"
  8. #include "winsapp.h"
  9. #include "windowslist.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. const int  PIXEL_PAD = 20;
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CMyListCtrl
  21.  
  22. CMyListCtrl::CMyListCtrl()
  23. {
  24.     m_cCols = 0;
  25.     m_pfnSort = NULL;
  26.  
  27.     m_pfnLCClick = NULL;
  28.     m_pfnLCColClick = NULL;
  29.     m_pfnLCDblClick = NULL;
  30. }
  31.  
  32. CMyListCtrl::~CMyListCtrl()
  33. {
  34. }
  35.  
  36.  
  37. BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
  38.     //{{AFX_MSG_MAP(CMyListCtrl)
  39.     ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
  40.     ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
  41.     ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
  42.     ON_WM_CHAR()
  43.     //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CMyListCtrl message handlers
  48.  
  49. void CMyListCtrl::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
  50. {
  51.     NMLISTVIEW* pNMListView = (NMLISTVIEW*)pNMHDR;
  52.     struct SortInfo si;
  53.  
  54.     si.iColumn = pNMListView->iSubItem;
  55.     si.pList = this;
  56.  
  57.     *pResult = 0;
  58.     if (m_pfnLCColClick)
  59.     {
  60.         m_pfnLCColClick(pNMHDR, pResult, m_lParamColClick, &si);
  61.     }
  62.     if (m_pfnSort && pNMListView)
  63.     {
  64.         SortItems(m_pfnSort, (LPARAM)&si);
  65.     }
  66. }
  67.  
  68. void CMyListCtrl::DoSort(int iCol)
  69. {
  70.     struct SortInfo si;
  71.  
  72.     si.iColumn = iCol + 1; 
  73.     si.pList = this;
  74.  
  75.     SortItems(m_pfnSort, (LPARAM)&si);
  76. }
  77.  
  78. void CMyListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult) 
  79. {
  80.     *pResult = 0;
  81.     if (m_pfnLCClick)
  82.     {
  83.         m_pfnLCClick(pNMHDR, pResult, m_lParamClick);
  84.     }
  85. }
  86.  
  87. int CMyListCtrl::InsertItem(int nItem, LPCTSTR lpszItem, LPARAM lItemData)
  88. {
  89.     int iRes;
  90.     iRes = CListCtrl::InsertItem(nItem, lpszItem);
  91.     SetItemData(nItem, lItemData);
  92.     return(iRes);
  93. }
  94.  
  95.  
  96.     
  97. int CMyListCtrl::GetCurrentIndex()
  98. {
  99.     // Get the current mouse location and convert it to client
  100.     // coordinates.
  101.     DWORD pos = GetMessagePos();
  102.     CPoint pt(LOWORD(pos), HIWORD(pos));
  103.     ScreenToClient(&pt);
  104.  
  105.     // Get indexes of the first and last visible items in listview
  106.     // control.
  107.     int index = GetTopIndex();
  108.     int last_visible_index = index + GetCountPerPage();
  109.     if (last_visible_index > GetItemCount())
  110.         last_visible_index = GetItemCount();
  111.  
  112.     // Loop until number visible items has been reached.
  113.     while (index <= last_visible_index)
  114.     {
  115.         // Get the bounding rectangle of an item. If the mouse
  116.         // location is within the bounding rectangle of the item,
  117.         // you know you have found the item that was being clicked.
  118.         CRect r;
  119.         GetItemRect(index, &r, LVIR_BOUNDS);
  120.         if (r.PtInRect(pt))
  121.         {
  122.             UINT flag = LVIS_SELECTED | LVIS_FOCUSED;
  123.             SetItemState(index, flag, flag);
  124.             break;
  125.         }
  126.  
  127.         // Get the next item in listview control.
  128.         index++;
  129.     }
  130.     return(index);
  131. }
  132.  
  133.  
  134. void CMyListCtrl::SetWidths()
  135. {
  136.     int cCols = GetColumnCount();
  137.     int *pWidths = new int[cCols];
  138.     int i, j, iWidth;
  139.     CString strItem;
  140.  
  141.     for (j = cCols-1; j >= 0; j--)
  142.     {
  143.         pWidths[j] = 0;
  144.     }
  145.     if (pWidths)
  146.     {
  147.         i = GetItemCount()-1;
  148.         for (;i >= 0; i--)
  149.         {
  150.             for (j = cCols-1; j >= 0; j--)
  151.             {
  152.                 GetItemText(i, j, strItem.GetBuffer(_MAX_PATH), _MAX_PATH);
  153.                 strItem.ReleaseBuffer();
  154.                 iWidth = GetStringWidth(strItem) + PIXEL_PAD;
  155.                 if (pWidths[j] < iWidth)
  156.                 {
  157.                     pWidths[j] = iWidth;
  158.                 }
  159.             }
  160.         }
  161.         for (j = cCols-1; j >= 0; j--)
  162.         {
  163.             iWidth = GetColumnWidth(j);
  164.             if (iWidth <= pWidths[j])
  165.             {
  166.                 iWidth = pWidths[j];
  167.             }
  168.             SetColumnWidth(j, iWidth);
  169.         }
  170.         delete[] pWidths;
  171.     }
  172. }
  173.  
  174. int CMyListCtrl::InsertColumn(int nCol, LPCTSTR lpszColumnHeading,
  175.         int nFormat /*= LVCFMT_LEFT*/, int nWidth /*= -1*/, int nSubItem /* = -1*/)
  176. {
  177.     m_cCols++;
  178.     int iWidth, iRes;
  179.  
  180.     iWidth = GetStringWidth(lpszColumnHeading) + PIXEL_PAD;
  181.     if (nWidth != -1)
  182.         iWidth = nWidth;
  183.     iRes = CListCtrl::InsertColumn(nCol, lpszColumnHeading, nFormat, iWidth, nSubItem);
  184.     SetColumnWidth(nCol, iWidth);
  185.  
  186.     return(iRes);
  187. }
  188.  
  189. int CMyListCtrl::GetColumnCount()
  190. {
  191.     return(m_cCols);
  192. }
  193.  
  194. void CMyListCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
  195. {
  196.     *pResult = 0;
  197.     if (m_pfnLCDblClick)
  198.     {
  199.         m_pfnLCDblClick(pNMHDR, pResult, m_lParamDblClick);
  200.     }
  201. }
  202.  
  203. void CMyListCtrl::SetSort(PFNLVCOMPARE pfnSort, LPARAM lParam)
  204. {
  205.     m_pfnSort = pfnSort;
  206.     m_lParamSort = lParam;
  207. }
  208.  
  209. void CMyListCtrl::SetOnClick(LPFNLCOP pfnLCOpHandler, LPARAM lParam)
  210. {
  211.     m_pfnLCClick = pfnLCOpHandler;
  212.     m_lParamClick = lParam;
  213. }
  214.  
  215. void CMyListCtrl::SetOnDblClick(LPFNLCOP pfnLCOpHandler, LPARAM lParam)
  216. {
  217.     m_pfnLCDblClick = pfnLCOpHandler;
  218.     m_lParamDblClick = lParam;
  219. }
  220.  
  221. void CMyListCtrl::SetColClick(LPFNLCSOP pfnLCOpHandler, LPARAM lParam)
  222. {
  223.     m_pfnLCColClick = pfnLCOpHandler;
  224.     m_lParamColClick = lParam;
  225. }
  226.  
  227.  
  228. void CMyListCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  229. {
  230.     // TODO: Add your message handler code here and/or call default
  231. //    if (nChar == Del)
  232. //    {
  233. //    }
  234.     CListCtrl::OnChar(nChar, nRepCnt, nFlags);
  235. }
  236.