winctrl3.cpp 22 KB

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