home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / WINCTRL3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  20.8 KB  |  810 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_CORE4_SEG
  14. #pragma code_seg(AFX_CORE4_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // _AFX_CHECKLIST_STATE
  26.  
  27. class _AFX_CHECKLIST_STATE : public CNoTrackObject
  28. {
  29. public:
  30.     _AFX_CHECKLIST_STATE();
  31.     virtual ~_AFX_CHECKLIST_STATE();
  32.  
  33.     HBITMAP m_hbitmapCheck;
  34.     CSize m_sizeCheck;
  35. };
  36.  
  37. _AFX_CHECKLIST_STATE::_AFX_CHECKLIST_STATE()
  38. {
  39.     CBitmap bitmap;
  40.  
  41.     if (afxData.bWin4 || AfxGetCtl3dState()->m_pfnSubclassDlgEx != NULL)
  42.         VERIFY(bitmap.LoadBitmap(AFX_IDB_CHECKLISTBOX_95));
  43.     else
  44.         VERIFY(bitmap.LoadBitmap(AFX_IDB_CHECKLISTBOX_NT));
  45.  
  46.     BITMAP bm;
  47.     bitmap.GetObject(sizeof (BITMAP), &bm);
  48.     m_sizeCheck.cx = bm.bmWidth / 3;
  49.     m_sizeCheck.cy = bm.bmHeight;
  50.     m_hbitmapCheck = (HBITMAP)bitmap.Detach();
  51. }
  52.  
  53. _AFX_CHECKLIST_STATE::~_AFX_CHECKLIST_STATE()
  54. {
  55.     if (m_hbitmapCheck != NULL)
  56.         ::DeleteObject(m_hbitmapCheck);
  57. }
  58.  
  59. EXTERN_PROCESS_LOCAL(_AFX_CHECKLIST_STATE, _afxChecklistState)
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // AFX_CHECK_DATA
  63.  
  64. struct AFX_CHECK_DATA
  65. {
  66. public:
  67.     int m_nCheck;
  68.     BOOL m_bEnabled;
  69.     DWORD m_dwUserData;
  70.  
  71.     AFX_CHECK_DATA()
  72.     {
  73.         m_nCheck = 0;
  74.         m_bEnabled = TRUE;
  75.         m_dwUserData = 0;
  76.     };
  77. };
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CCheckListBox
  81.  
  82. BEGIN_MESSAGE_MAP(CCheckListBox, CListBox)
  83.     //{{AFX_MSG_MAP(CCheckListBox)
  84.     ON_WM_LBUTTONDOWN()
  85.     ON_WM_KEYDOWN()
  86.     ON_WM_CREATE()
  87.     ON_WM_LBUTTONDBLCLK()
  88.     ON_MESSAGE(WM_SETFONT, OnSetFont)
  89.     ON_MESSAGE(LB_ADDSTRING, OnLBAddString)
  90.     ON_MESSAGE(LB_FINDSTRING, OnLBFindString)
  91.     ON_MESSAGE(LB_FINDSTRINGEXACT, OnLBFindStringExact)
  92.     ON_MESSAGE(LB_GETITEMDATA, OnLBGetItemData)
  93.     ON_MESSAGE(LB_GETTEXT, OnLBGetText)
  94.     ON_MESSAGE(LB_INSERTSTRING, OnLBInsertString)
  95.     ON_MESSAGE(LB_SELECTSTRING, OnLBSelectString)
  96.     ON_MESSAGE(LB_SETITEMDATA, OnLBSetItemData)
  97.     ON_MESSAGE(LB_SETITEMHEIGHT, OnLBSetItemHeight)
  98.     //}}AFX_MSG_MAP
  99. END_MESSAGE_MAP()
  100.  
  101. BOOL CCheckListBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
  102. {
  103.     if (!(dwStyle & LBS_OWNERDRAWVARIABLE)) //must be one or the other
  104.         dwStyle |= LBS_OWNERDRAWFIXED;
  105.     return CListBox::Create(dwStyle, rect, pParentWnd, nID);
  106. }
  107.  
  108. void CCheckListBox::SetCheckStyle(UINT nStyle)
  109. {
  110.     ASSERT(nStyle == 0 | nStyle == BS_CHECKBOX | nStyle == BS_AUTOCHECKBOX |
  111.         nStyle == BS_AUTO3STATE | nStyle == BS_3STATE);
  112.  
  113.     m_nStyle = nStyle;
  114. }
  115.  
  116. void CCheckListBox::SetCheck(int nIndex, int nCheck)
  117. {
  118.     ASSERT(::IsWindow(m_hWnd));
  119.  
  120.     if (nCheck == 2)
  121.     {
  122.         if (m_nStyle == BS_CHECKBOX || m_nStyle == BS_AUTOCHECKBOX)
  123.             return;
  124.     }
  125.  
  126.     LRESULT lResult = DefWindowProc(LB_GETITEMDATA, nIndex, 0);
  127.     if (lResult != LB_ERR)
  128.     {
  129.  
  130.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)lResult;
  131.  
  132.         if (pState == NULL)
  133.             pState = new AFX_CHECK_DATA;
  134.  
  135.         pState->m_nCheck = nCheck;
  136.         VERIFY(DefWindowProc(LB_SETITEMDATA, nIndex, (LPARAM)pState) != LB_ERR);
  137.  
  138.         InvalidateCheck(nIndex);
  139.     }
  140. }
  141.  
  142. int CCheckListBox::GetCheck(int nIndex)
  143. {
  144.     ASSERT(::IsWindow(m_hWnd));
  145.  
  146.     LRESULT lResult = DefWindowProc(LB_GETITEMDATA, nIndex, 0);
  147.     if (lResult != LB_ERR)
  148.     {
  149.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)lResult;
  150.         if (pState != NULL)
  151.             return pState->m_nCheck;
  152.     }
  153.     return 0; // The default
  154. }
  155.  
  156. void CCheckListBox::Enable(int nIndex, BOOL bEnabled)
  157. {
  158.     ASSERT(::IsWindow(m_hWnd));
  159.  
  160.     LRESULT lResult = DefWindowProc(LB_GETITEMDATA, nIndex, 0);
  161.     if (lResult != LB_ERR)
  162.     {
  163.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)lResult;
  164.  
  165.         if (pState == NULL)
  166.             pState = new AFX_CHECK_DATA;
  167.  
  168.         pState->m_bEnabled = bEnabled;
  169.         VERIFY(DefWindowProc(LB_SETITEMDATA, nIndex, (LPARAM)pState) != LB_ERR);
  170.  
  171.         InvalidateItem(nIndex);
  172.     }
  173. }
  174.  
  175. int CCheckListBox::IsEnabled(int nIndex)
  176. {
  177.     ASSERT(::IsWindow(m_hWnd));
  178.  
  179.     LRESULT lResult = DefWindowProc(LB_GETITEMDATA, nIndex, 0);
  180.     if (lResult != LB_ERR)
  181.     {
  182.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)lResult;
  183.         if (pState != NULL)
  184.             return pState->m_bEnabled;
  185.     }
  186.     return TRUE; // The default
  187. }
  188.  
  189. CRect CCheckListBox::OnGetCheckPosition(CRect, CRect rectCheckBox)
  190. {
  191.     return rectCheckBox;
  192. }
  193.  
  194. void CCheckListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  195. {
  196.     // You must override DrawItem and MeasureItem for LBS_OWNERDRAWVARIABLE
  197.     ASSERT((GetStyle() & (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS)) ==
  198.         (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS));
  199.  
  200.     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  201.  
  202.     if (((LONG)(lpDrawItemStruct->itemID) >= 0) &&
  203.         (lpDrawItemStruct->itemAction & (ODA_DRAWENTIRE | ODA_SELECT)))
  204.     {
  205.         int cyItem = GetItemHeight(lpDrawItemStruct->itemID);
  206.         BOOL fDisabled = !IsWindowEnabled() || !IsEnabled(lpDrawItemStruct->itemID);
  207.  
  208.         COLORREF newTextColor = fDisabled ?
  209.             RGB(0x80, 0x80, 0x80) : GetSysColor(COLOR_WINDOWTEXT);  // light gray
  210.         COLORREF oldTextColor = pDC->SetTextColor(newTextColor);
  211.  
  212.         COLORREF newBkColor = GetSysColor(COLOR_WINDOW);
  213.         COLORREF oldBkColor = pDC->SetBkColor(newBkColor);
  214.  
  215.         if (newTextColor == newBkColor)
  216.             newTextColor = RGB(0xC0, 0xC0, 0xC0);   // dark gray
  217.  
  218.         if (!fDisabled && ((lpDrawItemStruct->itemState & ODS_SELECTED) != 0))
  219.         {
  220.             pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
  221.             pDC->SetBkColor(GetSysColor(COLOR_HIGHLIGHT));
  222.         }
  223.  
  224.         if (m_cyText == 0)
  225.             VERIFY(cyItem >= CalcMinimumItemHeight());
  226.  
  227.         CString strText;
  228.         GetText(lpDrawItemStruct->itemID, strText);
  229.  
  230.         pDC->ExtTextOut(lpDrawItemStruct->rcItem.left,
  231.             lpDrawItemStruct->rcItem.top + max(0, (cyItem - m_cyText) / 2),
  232.             ETO_OPAQUE, &(lpDrawItemStruct->rcItem), strText, strText.GetLength(), NULL);
  233.  
  234.         pDC->SetTextColor(oldTextColor);
  235.         pDC->SetBkColor(oldBkColor);
  236.     }
  237.  
  238.     if ((lpDrawItemStruct->itemAction & ODA_FOCUS) != 0)
  239.         pDC->DrawFocusRect(&(lpDrawItemStruct->rcItem));
  240. }
  241.  
  242. void CCheckListBox::MeasureItem(LPMEASUREITEMSTRUCT)
  243. {
  244.     // You must override DrawItem and MeasureItem for LBS_OWNERDRAWVARIABLE
  245.     ASSERT((GetStyle() & (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS)) ==
  246.         (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS));
  247. }
  248.  
  249. void CCheckListBox::PreDrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  250. {
  251.     _AFX_CHECKLIST_STATE* pChecklistState = _afxChecklistState;
  252.  
  253.     DRAWITEMSTRUCT drawItem;
  254.     memcpy(&drawItem, lpDrawItemStruct, sizeof(DRAWITEMSTRUCT));
  255.  
  256.     if ((((LONG)drawItem.itemID) >= 0) &&
  257.        ((drawItem.itemAction & (ODA_DRAWENTIRE | ODA_SELECT)) != 0))
  258.     {
  259.         int cyItem = GetItemHeight(drawItem.itemID);
  260.  
  261.         CDC* pDC = CDC::FromHandle(drawItem.hDC);
  262.  
  263.         COLORREF newBkColor = GetSysColor(COLOR_WINDOW);
  264.  
  265.         BOOL fDisabled = !IsWindowEnabled() || !IsEnabled(drawItem.itemID);
  266.         if ((drawItem.itemState & ODS_SELECTED) && !fDisabled)
  267.             newBkColor = GetSysColor(COLOR_HIGHLIGHT);
  268.  
  269.         COLORREF oldBkColor = pDC->SetBkColor(newBkColor);
  270.  
  271.         CDC bitmapDC;
  272.         if (bitmapDC.CreateCompatibleDC(pDC))
  273.         {
  274.             int nCheck = GetCheck(drawItem.itemID);
  275.             HBITMAP hOldBitmap = (HBITMAP)::SelectObject(bitmapDC.m_hDC, pChecklistState->m_hbitmapCheck);
  276.  
  277.             CRect rectCheck = drawItem.rcItem;
  278.             rectCheck.left += 1;
  279.             rectCheck.top += 1 + max(0, (cyItem - pChecklistState->m_sizeCheck.cy) / 2);
  280.             rectCheck.right = rectCheck.left + pChecklistState->m_sizeCheck.cx;
  281.             rectCheck.bottom = rectCheck.top + pChecklistState->m_sizeCheck.cy;
  282.  
  283.             CRect rectItem = drawItem.rcItem;
  284.             rectItem.right = rectItem.left + pChecklistState->m_sizeCheck.cx + 2;
  285.  
  286.             CRect rectCheckBox = OnGetCheckPosition(rectItem, rectCheck);
  287.  
  288.             ASSERT(rectCheck.IntersectRect(rectItem, rectCheckBox));
  289.             ASSERT((rectCheck == rectCheckBox) && (rectCheckBox.Size() == pChecklistState->m_sizeCheck));
  290.  
  291.             CBrush brush(newBkColor);
  292.             pDC->FillRect(rectItem, &brush);
  293.  
  294.             pDC->BitBlt(rectCheckBox.left, rectCheckBox.top,
  295.                 pChecklistState->m_sizeCheck.cx, pChecklistState->m_sizeCheck.cy, &bitmapDC,
  296.                 pChecklistState->m_sizeCheck.cx  * nCheck, 0, SRCCOPY);
  297.  
  298.             ::SelectObject(bitmapDC.m_hDC, hOldBitmap);
  299.         }
  300.         pDC->SetBkColor(oldBkColor);
  301.     }
  302.  
  303.     if (drawItem.itemData != 0)
  304.     {
  305.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)drawItem.itemData;
  306.         drawItem.itemData = pState->m_dwUserData;
  307.     }
  308.     drawItem.rcItem.left = drawItem.rcItem.left + pChecklistState->m_sizeCheck.cx + 2;
  309.  
  310.     DrawItem(&drawItem);
  311. }
  312.  
  313. void CCheckListBox::PreMeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  314. {
  315.     int cyItem = CalcMinimumItemHeight();
  316.  
  317.     MEASUREITEMSTRUCT measureItem;
  318.     memcpy(&measureItem, lpMeasureItemStruct, sizeof(MEASUREITEMSTRUCT));
  319.  
  320.     measureItem.itemHeight = cyItem;
  321.     measureItem.itemWidth  = (UINT)-1;
  322.  
  323.     // WINBUG: Windows95 and Windows NT disagree on what this value
  324.     // should be.  According to the docs, they are both wrong
  325.     if (GetStyle() & LBS_OWNERDRAWVARIABLE)
  326.     {
  327.         LRESULT lResult = DefWindowProc(LB_GETITEMDATA, measureItem.itemID, 0);
  328.         if (lResult != LB_ERR)
  329.             measureItem.itemData = (UINT)lResult;
  330.         else
  331.             measureItem.itemData = 0;
  332.  
  333.         // WINBUG: This is only done in the LBS_OWNERDRAWVARIABLE case
  334.         // because Windows 95 does not initialize itemData to zero in the
  335.         // case of LBS_OWNERDRAWFIXED list boxes (it is stack garbage).
  336.         if (measureItem.itemData != 0)
  337.         {
  338.             AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)measureItem.itemData;
  339.             measureItem.itemData = pState->m_dwUserData;
  340.         }
  341.     }
  342.  
  343.     MeasureItem(&measureItem);
  344.  
  345.     lpMeasureItemStruct->itemHeight = max(measureItem.itemHeight,(UINT) cyItem);
  346.     lpMeasureItemStruct->itemWidth = measureItem.itemWidth;
  347. }
  348.  
  349. int CCheckListBox::PreCompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
  350. {
  351.     COMPAREITEMSTRUCT compareItem;
  352.     memcpy(&compareItem, lpCompareItemStruct, sizeof(COMPAREITEMSTRUCT));
  353.  
  354.     if (compareItem.itemData1 != 0)
  355.     {
  356.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)compareItem.itemData1;
  357.         compareItem.itemData1 = pState->m_dwUserData;
  358.     }
  359.     if (compareItem.itemData2 != 0)
  360.     {
  361.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)compareItem.itemData2;
  362.         compareItem.itemData2 = pState->m_dwUserData;
  363.     }
  364.     return CompareItem(&compareItem);
  365. }
  366.  
  367. void CCheckListBox::PreDeleteItem(LPDELETEITEMSTRUCT lpDeleteItemStruct)
  368. {
  369.     DELETEITEMSTRUCT deleteItem;
  370.     memcpy(&deleteItem, lpDeleteItemStruct, sizeof(DELETEITEMSTRUCT));
  371.  
  372.     // WINBUG: The following if block is required because Windows NT
  373.     // version 3.51 does not properly fill out the LPDELETEITEMSTRUCT.
  374.     if (deleteItem.itemData == 0)
  375.     {
  376.         LRESULT lResult = DefWindowProc(LB_GETITEMDATA, deleteItem.itemID, 0);
  377.         if (lResult != LB_ERR)
  378.             deleteItem.itemData = (UINT)lResult;
  379.     }
  380.  
  381.     if (deleteItem.itemData != 0)
  382.     {
  383.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)deleteItem.itemData;
  384.         deleteItem.itemData = pState->m_dwUserData;
  385.         delete pState;
  386.     }
  387.     DeleteItem(&deleteItem);
  388. }
  389.  
  390. BOOL CCheckListBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam,
  391.     LRESULT* pResult)
  392. {
  393.     switch (message)
  394.     {
  395.     case WM_DRAWITEM:
  396.         ASSERT(pResult == NULL);       // no return value expected
  397.         PreDrawItem((LPDRAWITEMSTRUCT)lParam);
  398.         break;
  399.     case WM_MEASUREITEM:
  400.         ASSERT(pResult == NULL);       // no return value expected
  401.         PreMeasureItem((LPMEASUREITEMSTRUCT)lParam);
  402.         break;
  403.     case WM_COMPAREITEM:
  404.         ASSERT(pResult != NULL);       // return value expected
  405.         *pResult = PreCompareItem((LPCOMPAREITEMSTRUCT)lParam);
  406.         break;
  407.     case WM_DELETEITEM:
  408.         ASSERT(pResult == NULL);       // no return value expected
  409.         PreDeleteItem((LPDELETEITEMSTRUCT)lParam);
  410.         break;
  411.     default:
  412.         return CListBox::OnChildNotify(message, wParam, lParam, pResult);
  413.     }
  414.     return TRUE;
  415. }
  416.  
  417. #ifdef _DEBUG
  418. void CCheckListBox::PreSubclassWindow()
  419. {
  420.     CListBox::PreSubclassWindow();
  421.  
  422.     // CCheckListBoxes must be owner drawn
  423.     ASSERT(GetStyle() & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE));
  424. }
  425. #endif
  426.  
  427. int CCheckListBox::CalcMinimumItemHeight()
  428. {
  429.     int nResult;
  430.  
  431.     _AFX_CHECKLIST_STATE* pChecklistState = _afxChecklistState;
  432.  
  433.     if ((GetStyle() & (LBS_HASSTRINGS | LBS_OWNERDRAWFIXED)) ==
  434.         (LBS_HASSTRINGS | LBS_OWNERDRAWFIXED))
  435.     {
  436.         CClientDC dc(this);
  437.         CFont* pOldFont = dc.SelectObject(GetFont());
  438.         TEXTMETRIC tm;
  439.         VERIFY (dc.GetTextMetrics ( &tm ));
  440.         dc.SelectObject(pOldFont);
  441.  
  442.         m_cyText = tm.tmHeight;
  443.         nResult = max(pChecklistState->m_sizeCheck.cy + 1, m_cyText);
  444.     }
  445.     else
  446.     {
  447.         nResult = pChecklistState->m_sizeCheck.cy + 1;
  448.     }
  449.  
  450.     return nResult;
  451. }
  452.  
  453. void CCheckListBox::InvalidateCheck(int nIndex)
  454. {
  455.     CRect rect;
  456.     _AFX_CHECKLIST_STATE* pChecklistState = _afxChecklistState;
  457.  
  458.     GetItemRect(nIndex, rect);
  459.     rect.right = rect.left + pChecklistState->m_sizeCheck.cx + 2;
  460.     InvalidateRect(rect, FALSE);
  461. }
  462.  
  463. void CCheckListBox::InvalidateItem(int nIndex)
  464. {
  465.     CRect rect;
  466.     GetItemRect(nIndex, rect);
  467.     InvalidateRect(rect, FALSE);
  468. }
  469.  
  470. int CCheckListBox::CheckFromPoint(CPoint point, BOOL& bInCheck)
  471. {
  472.     // assume did not hit anything
  473.     bInCheck = FALSE;
  474.     int nIndex = -1;
  475.  
  476.     _AFX_CHECKLIST_STATE* pChecklistState = _afxChecklistState;
  477.     if ((GetStyle() & (LBS_OWNERDRAWFIXED|LBS_MULTICOLUMN)) == LBS_OWNERDRAWFIXED)
  478.     {
  479.         // optimized case for ownerdraw fixed, single column
  480.         int cyItem = GetItemHeight(0);
  481.         if (point.y < cyItem * GetCount())
  482.         {
  483.             nIndex = GetTopIndex() + point.y / cyItem;
  484.             if (point.x < pChecklistState->m_sizeCheck.cx + 2)
  485.                 ++bInCheck;
  486.         }
  487.     }
  488.     else
  489.     {
  490.         // general case for ownerdraw variable or multiple column
  491.         for (int i = GetTopIndex(); i < GetCount(); i++)
  492.         {
  493.             CRect itemRect;
  494.             GetItemRect(i, &itemRect);
  495.             if (itemRect.PtInRect(point))
  496.             {
  497.                 nIndex = i;
  498.                 if (point.x < itemRect.left + pChecklistState->m_sizeCheck.cx + 2)
  499.                     ++bInCheck;
  500.                 break;
  501.             }
  502.         }
  503.     }
  504.     return nIndex;
  505. }
  506.  
  507. void CCheckListBox::OnLButtonDown(UINT nFlags, CPoint point)
  508. {
  509.     SetFocus();
  510.  
  511.     // determine where the click is
  512.     BOOL bInCheck;
  513.     int nIndex = CheckFromPoint(point, bInCheck);
  514.  
  515.     // if the item is disabled, then eat the click
  516.     if (!IsEnabled(nIndex))
  517.         return;
  518.  
  519.     if (m_nStyle != BS_CHECKBOX && m_nStyle != BS_3STATE)
  520.     {
  521.         // toggle check mark automatically if check mark was hit
  522.         if (bInCheck)
  523.         {
  524.             CWnd*   pParent = GetParent();
  525.             ASSERT_VALID(pParent);
  526.  
  527.             int nModulo = (m_nStyle == BS_AUTO3STATE) ? 3 : 2;
  528.             int nCheck = GetCheck(nIndex);
  529.             nCheck = (nCheck == nModulo) ? nCheck - 1 : nCheck;
  530.             SetCheck(nIndex, (nCheck + 1) % nModulo);
  531.  
  532.             InvalidateCheck(nIndex);
  533.  
  534.             CListBox::OnLButtonDown(nFlags, point);
  535.  
  536.             // Inform of check
  537.             pParent->SendMessage(WM_COMMAND,
  538.                 MAKEWPARAM(GetDlgCtrlID(), CLBN_CHKCHANGE),
  539.                 (LPARAM)m_hWnd);
  540.             return;
  541.         }
  542.     }
  543.  
  544.     // do default listbox selection logic
  545.     CListBox::OnLButtonDown(nFlags, point);
  546. }
  547.  
  548. void CCheckListBox::OnLButtonDblClk(UINT nFlags, CPoint point)
  549. {
  550.     BOOL bInCheck;
  551.     CheckFromPoint(point, bInCheck);
  552.  
  553.     if (bInCheck)
  554.     {
  555.         // Double and single clicks act the same on the check box!
  556.         OnLButtonDown(nFlags, point);
  557.         return;
  558.     }
  559.  
  560.     CListBox::OnLButtonDblClk(nFlags, point);
  561. }
  562.  
  563. void CCheckListBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  564. {
  565.     if (nChar == VK_SPACE)
  566.     {
  567.         int nIndex = GetCaretIndex();
  568.         CWnd* pParent = GetParent();
  569.         ASSERT_VALID(pParent);
  570.  
  571.         if (nIndex != LB_ERR)
  572.         {
  573.             if (m_nStyle != BS_CHECKBOX && m_nStyle != BS_3STATE)
  574.             {
  575.                 if ((GetStyle() & LBS_MULTIPLESEL) != 0)
  576.                 {
  577.                     if (IsEnabled(nIndex))
  578.                     {
  579.                         BOOL bSelected = GetSel(nIndex);
  580.                         if (bSelected)
  581.                         {
  582.                             int nModulo = (m_nStyle == BS_AUTO3STATE) ? 3 : 2;
  583.                             int nCheck = GetCheck(nIndex);
  584.                             nCheck = (nCheck == nModulo) ? nCheck - 1 : nCheck;
  585.                             SetCheck(nIndex, (nCheck + 1) % nModulo);
  586.  
  587.                             // Inform of check
  588.                             pParent->SendMessage(WM_COMMAND,
  589.                                 MAKEWPARAM(GetDlgCtrlID(), CLBN_CHKCHANGE),
  590.                                 (LPARAM)m_hWnd);
  591.                         }
  592.                         SetSel(nIndex, !bSelected);
  593.                     }
  594.                     else
  595.                         SetSel(nIndex, FALSE); // unselect disabled items
  596.  
  597.                     return;
  598.                 }
  599.                 else
  600.                 {
  601.                     // If there is a selection, the space bar toggles that check,
  602.                     // all other keys are the same as a standard listbox.
  603.  
  604.                     if (IsEnabled(nIndex))
  605.                     {
  606.                         int nModulo = (m_nStyle == BS_AUTO3STATE) ? 3 : 2;
  607.                         int nCheck = GetCheck(nIndex);
  608.                         nCheck = (nCheck == nModulo) ? nCheck - 1 : nCheck;
  609.                         SetCheck(nIndex, (nCheck + 1) % nModulo);
  610.  
  611.                         InvalidateCheck(nIndex);
  612.  
  613.                         // Inform of check
  614.                         pParent->SendMessage(WM_COMMAND,
  615.                             MAKEWPARAM(GetDlgCtrlID(), CLBN_CHKCHANGE),
  616.                             (LPARAM)m_hWnd);
  617.                     }
  618.                     else
  619.                         SetSel(nIndex, FALSE); // unselect disabled items
  620.  
  621.                     return;
  622.                 }
  623.             }
  624.         }
  625.     }
  626.     CListBox::OnKeyDown(nChar, nRepCnt, nFlags);
  627. }
  628.  
  629. int CCheckListBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
  630. {
  631.     if (CListBox::OnCreate(lpCreateStruct) == -1)
  632.         return -1;
  633.  
  634.     if ((GetStyle() & (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS))
  635.         == (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS))
  636.         SetItemHeight(0, CalcMinimumItemHeight());
  637.  
  638.     return 0;
  639. }
  640.  
  641. LRESULT CCheckListBox::OnSetFont(WPARAM , LPARAM)
  642. {
  643.     Default();
  644.  
  645.     if ((GetStyle() & (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS))
  646.         == (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS))
  647.         SetItemHeight(0, CalcMinimumItemHeight());
  648.  
  649.     return 0;
  650. }
  651.  
  652. LRESULT CCheckListBox::OnLBAddString(WPARAM wParam, LPARAM lParam)
  653. {
  654.     AFX_CHECK_DATA* pState = NULL;
  655.  
  656.     if (!(GetStyle() & LBS_HASSTRINGS))
  657.     {
  658.         pState = new AFX_CHECK_DATA;
  659.  
  660.         pState->m_dwUserData = lParam;
  661.         lParam = (LPARAM)pState;
  662.     }
  663.  
  664.     LRESULT lResult = DefWindowProc(LB_ADDSTRING, wParam, lParam);
  665.  
  666.     if (lResult == LB_ERR && pState != NULL)
  667.         delete pState;
  668.  
  669.     return lResult;
  670. }
  671.  
  672. LRESULT CCheckListBox::OnLBFindString(WPARAM wParam, LPARAM lParam)
  673. {
  674.     if (GetStyle() & LBS_HASSTRINGS)
  675.         return DefWindowProc(LB_FINDSTRING, wParam, lParam);
  676.  
  677.     int nIndex = wParam;
  678.     if (nIndex == -1) nIndex = 0;
  679.  
  680.     for(; nIndex < GetCount(); nIndex++)
  681.         if ((DWORD)lParam == GetItemData(nIndex))
  682.             return nIndex;
  683.  
  684.     return LB_ERR;
  685. }
  686.  
  687. LRESULT CCheckListBox::OnLBFindStringExact(WPARAM wParam, LPARAM lParam)
  688. {
  689.     if (GetStyle() & (LBS_HASSTRINGS | LBS_SORT))
  690.         return DefWindowProc(LB_FINDSTRINGEXACT, wParam, lParam);
  691.  
  692.     int nIndex = wParam;
  693.     if (nIndex == -1) nIndex = 0;
  694.  
  695.     for(; nIndex < GetCount(); nIndex++)
  696.         if ((DWORD)lParam == GetItemData(nIndex))
  697.             return nIndex;
  698.  
  699.     return LB_ERR;
  700. }
  701.  
  702. LRESULT CCheckListBox::OnLBGetItemData(WPARAM wParam, LPARAM lParam)
  703. {
  704.     LRESULT lResult = DefWindowProc(LB_GETITEMDATA, wParam, lParam);
  705.  
  706.     if (lResult != LB_ERR)
  707.     {
  708.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)lResult;
  709.  
  710.         if (pState == NULL)
  711.             return 0; // default
  712.  
  713.         lResult = pState->m_dwUserData;
  714.     }
  715.     return lResult;
  716. }
  717.  
  718. LRESULT CCheckListBox::OnLBGetText(WPARAM wParam, LPARAM lParam)
  719. {
  720.     LRESULT lResult = DefWindowProc(LB_GETTEXT, wParam, lParam);
  721.  
  722.     if (GetStyle() & LBS_HASSTRINGS)
  723.         return lResult;
  724.  
  725.     if (lResult != LB_ERR)
  726.     {
  727.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)lParam;
  728.  
  729.         if (pState != NULL)
  730.             lParam = pState->m_dwUserData;
  731.     }
  732.     return lResult;
  733. }
  734.  
  735. LRESULT CCheckListBox::OnLBInsertString(WPARAM wParam, LPARAM lParam)
  736. {
  737.     AFX_CHECK_DATA* pState = NULL;
  738.  
  739.     if (!(GetStyle() & LBS_HASSTRINGS))
  740.     {
  741.         pState = new AFX_CHECK_DATA;
  742.         pState->m_dwUserData = lParam;
  743.         lParam = (LPARAM)pState;
  744.     }
  745.  
  746.     LRESULT lResult = DefWindowProc(LB_INSERTSTRING, wParam, lParam);
  747.  
  748.     if (lResult == LB_ERR && pState != NULL)
  749.         delete pState;
  750.  
  751.     return lResult;
  752. }
  753.  
  754. LRESULT CCheckListBox::OnLBSelectString(WPARAM wParam, LPARAM lParam)
  755. {
  756.     if (GetStyle() & LBS_HASSTRINGS)
  757.         return DefWindowProc(LB_SELECTSTRING, wParam, lParam);
  758.  
  759.     int nIndex = wParam;
  760.     if (nIndex == -1) nIndex = 0;
  761.  
  762.     for(; nIndex < GetCount(); nIndex++)
  763.         if ((DWORD)lParam == GetItemData(nIndex))
  764.         {
  765.             SetCurSel(nIndex);
  766.             return nIndex;
  767.         }
  768.  
  769.     return LB_ERR;
  770. }
  771.  
  772. LRESULT CCheckListBox::OnLBSetItemData(WPARAM wParam, LPARAM lParam)
  773. {
  774.     LRESULT lResult = DefWindowProc(LB_GETITEMDATA, wParam, 0);
  775.  
  776.     if (lResult != LB_ERR)
  777.     {
  778.         AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)lResult;
  779.  
  780.         if (pState == NULL)
  781.             pState = new AFX_CHECK_DATA;
  782.  
  783.         pState->m_dwUserData = lParam;
  784.         lResult = DefWindowProc(LB_SETITEMDATA, wParam, (LPARAM)pState);
  785.  
  786.         if (lResult == LB_ERR)
  787.             delete pState;
  788.     }
  789.     return lResult;
  790. }
  791.  
  792. LRESULT CCheckListBox::OnLBSetItemHeight(WPARAM wParam, LPARAM lParam)
  793. {
  794.     int nHeight = max(CalcMinimumItemHeight(),(int)LOWORD(lParam));
  795.     return DefWindowProc(LB_SETITEMHEIGHT, wParam, MAKELPARAM(nHeight,0));
  796. }
  797.  
  798. #ifdef AFX_INIT_SEG
  799. #pragma code_seg(AFX_INIT_SEG)
  800. #endif
  801.  
  802. IMPLEMENT_DYNAMIC(CCheckListBox, CListBox)
  803.  
  804. #pragma warning(disable: 4074)
  805. #pragma init_seg(lib)
  806.  
  807. PROCESS_LOCAL(_AFX_CHECKLIST_STATE, _afxChecklistState)
  808.  
  809. /////////////////////////////////////////////////////////////////////////////
  810.