XTCheckListBox.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:27k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTCheckListBox.cpp : implementation of the CXTCheckListBox class.
  2. //
  3. // This file is a part of the XTREME CONTROLS MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "Resource.h"
  22. #include "Common/XTPVC80Helpers.h"  // Visual Studio 2005 helper functions
  23. #include "Common/XTPResourceManager.h"
  24. #include "Common/XTPWinThemeWrapper.h"
  25. #include "XTGlobal.h"
  26. #include "XTListBox.h"
  27. #include "XTCheckListBox.h"
  28. #ifndef LAYOUT_BITMAPORIENTATIONPRESERVED
  29. #define LAYOUT_BITMAPORIENTATIONPRESERVED  0x00000008
  30. #endif
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CXTCheckListState
  38. CXTCheckListBox::CCheckListState::CCheckListState(BOOL bListBox3D)
  39. {
  40. CBitmap bitmap;
  41. VERIFY(XTPResourceManager()->LoadBitmap(&bitmap, !bListBox3D ? XT_IDB_CHECKLISTBOX : XT_IDB_CHECKLISTBOX_3D));
  42. BITMAP bm;
  43. bitmap.GetObject(sizeof(BITMAP), &bm);
  44. m_sizeCheck.cx = bm.bmWidth / 3;
  45. m_sizeCheck.cy = bm.bmHeight;
  46. m_hbitmapCheck = (HBITMAP)bitmap.Detach();
  47. }
  48. CXTCheckListBox::CCheckListState::~CCheckListState()
  49. {
  50. if (m_hbitmapCheck != NULL)
  51. ::DeleteObject(m_hbitmapCheck);
  52. }
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CHECK_DATA
  55. struct CXTCheckListBox::CHECK_DATA
  56. {
  57. public:
  58. int m_nCheck;
  59. BOOL m_bEnabled;
  60. DWORD_PTR m_dwUserData;
  61. CHECK_DATA()
  62. {
  63. m_nCheck = 0;
  64. m_bEnabled = TRUE;
  65. m_dwUserData = 0;
  66. }
  67. };
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CXTCheckListBox
  70. CXTCheckListBox::CXTCheckListBox(BOOL bListBox3D /* = FALSE */)
  71. : m_checkListState(bListBox3D)
  72. , m_cyText(0)
  73. , m_nStyle(0)
  74. {
  75. }
  76. CXTCheckListBox::~CXTCheckListBox()
  77. {
  78. }
  79. IMPLEMENT_DYNAMIC(CXTCheckListBox, CXTListBox)
  80. BEGIN_MESSAGE_MAP(CXTCheckListBox, CXTListBox)
  81. //{{AFX_MSG_MAP(CXTCheckListBox)
  82. ON_WM_LBUTTONDOWN()
  83. ON_WM_KEYDOWN()
  84. ON_WM_CREATE()
  85. ON_WM_LBUTTONDBLCLK()
  86. ON_MESSAGE(WM_SETFONT, OnSetFont)
  87. ON_MESSAGE(LB_ADDSTRING, OnLBAddString)
  88. ON_MESSAGE(LB_FINDSTRING, OnLBFindString)
  89. ON_MESSAGE(LB_FINDSTRINGEXACT, OnLBFindStringExact)
  90. ON_MESSAGE(LB_GETITEMDATA, OnLBGetItemData)
  91. ON_MESSAGE(LB_GETTEXT, OnLBGetText)
  92. ON_MESSAGE(LB_INSERTSTRING, OnLBInsertString)
  93. ON_MESSAGE(LB_SELECTSTRING, OnLBSelectString)
  94. ON_MESSAGE(LB_SETITEMDATA, OnLBSetItemData)
  95. ON_MESSAGE(LB_SETITEMHEIGHT, OnLBSetItemHeight)
  96. //}}AFX_MSG_MAP
  97. END_MESSAGE_MAP()
  98. BOOL CXTCheckListBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
  99. {
  100. if (!(dwStyle & LBS_OWNERDRAWVARIABLE)) //must be one or the other
  101. {
  102. dwStyle |= LBS_OWNERDRAWFIXED;
  103. }
  104. return CXTListBox::Create(dwStyle, rect, pParentWnd, nID);
  105. }
  106. void CXTCheckListBox::SetCheckStyle(UINT nStyle)
  107. {
  108. ASSERT(
  109. nStyle == 0 ||
  110. nStyle == BS_CHECKBOX ||
  111. nStyle == BS_AUTOCHECKBOX ||
  112. nStyle == BS_AUTO3STATE ||
  113. nStyle == BS_3STATE);
  114. m_nStyle = nStyle;
  115. }
  116. void CXTCheckListBox::SetCheck(int nIndex, int nCheck)
  117. {
  118. ASSERT(::IsWindow(m_hWnd));
  119. if (nCheck == 2)
  120. {
  121. if (m_nStyle == BS_CHECKBOX || m_nStyle == BS_AUTOCHECKBOX)
  122. return;
  123. }
  124. LRESULT lResult = DefWindowProc(LB_GETITEMDATA, nIndex, 0);
  125. if (lResult != LB_ERR)
  126. {
  127. CHECK_DATA* pState = (CHECK_DATA*)lResult;
  128. if (pState == NULL)
  129. pState = new CHECK_DATA;
  130. pState->m_nCheck = nCheck;
  131. VERIFY(DefWindowProc(LB_SETITEMDATA, nIndex, (LPARAM)pState) != LB_ERR);
  132. InvalidateCheck(nIndex);
  133. }
  134. }
  135. int CXTCheckListBox::GetCheck(int nIndex)
  136. {
  137. ASSERT(::IsWindow(m_hWnd));
  138. LRESULT lResult = DefWindowProc(LB_GETITEMDATA, nIndex, 0);
  139. if (lResult != LB_ERR)
  140. {
  141. CHECK_DATA* pState = (CHECK_DATA*)lResult;
  142. if (pState != NULL)
  143. return pState->m_nCheck;
  144. }
  145. return 0; // The default
  146. }
  147. void CXTCheckListBox::Enable(int nIndex, BOOL bEnabled)
  148. {
  149. ASSERT(::IsWindow(m_hWnd));
  150. LRESULT lResult = DefWindowProc(LB_GETITEMDATA, nIndex, 0);
  151. if (lResult != LB_ERR)
  152. {
  153. CHECK_DATA* pState = (CHECK_DATA*)lResult;
  154. if (pState == NULL)
  155. pState = new CHECK_DATA;
  156. pState->m_bEnabled = bEnabled;
  157. VERIFY(DefWindowProc(LB_SETITEMDATA, nIndex, (LPARAM)pState) != LB_ERR);
  158. InvalidateItem(nIndex);
  159. }
  160. }
  161. int CXTCheckListBox::IsEnabled(int nIndex)
  162. {
  163. ASSERT(::IsWindow(m_hWnd));
  164. LRESULT lResult = DefWindowProc(LB_GETITEMDATA, nIndex, 0);
  165. if (lResult != LB_ERR)
  166. {
  167. CHECK_DATA* pState = (CHECK_DATA*)lResult;
  168. if (pState != NULL)
  169. return pState->m_bEnabled;
  170. }
  171. return TRUE; // The default
  172. }
  173. CRect CXTCheckListBox::OnGetCheckPosition(CRect, CRect rectCheckBox)
  174. {
  175. return rectCheckBox;
  176. }
  177. void CXTCheckListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  178. {
  179. // Must be LBS_OWNERDRAWVARIABLE or LBS_OWNERDRAWFIXED and LBS_HASSTRINGS
  180. ASSERT(GetStyle() & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE));
  181. ASSERT(GetStyle() & (LBS_HASSTRINGS));
  182. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  183. CRect rcItem(lpDIS->rcItem);
  184. rcItem.left += 2;
  185. if (((LONG)(lpDIS->itemID) >= 0) &&
  186. (lpDIS->itemAction & (ODA_DRAWENTIRE | ODA_SELECT)))
  187. {
  188. int cyItem = GetItemHeight(lpDIS->itemID);
  189. BOOL fDisabled = !IsWindowEnabled() || !IsEnabled(lpDIS->itemID);
  190. COLORREF newTextColor = fDisabled ?
  191. RGB(0x80, 0x80, 0x80) : GetSysColor(COLOR_WINDOWTEXT);  // light gray
  192. COLORREF oldTextColor = pDC->SetTextColor(newTextColor);
  193. COLORREF newBkColor = GetSysColor(COLOR_WINDOW);
  194. COLORREF oldBkColor = pDC->SetBkColor(newBkColor);
  195. if (newTextColor == newBkColor)
  196. newTextColor = RGB(0xC0, 0xC0, 0xC0);   // dark gray
  197. if (!fDisabled && ((lpDIS->itemState & ODS_SELECTED) != 0))
  198. {
  199. pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
  200. pDC->SetBkColor(GetSysColor(COLOR_HIGHLIGHT));
  201. }
  202. if (m_cyText == 0)
  203. VERIFY(cyItem >= CalcMinimumItemHeight());
  204. CString strText;
  205. GetText(lpDIS->itemID, strText);
  206. pDC->ExtTextOut(rcItem.left + 2, rcItem.top,
  207. ETO_OPAQUE, &rcItem, strText, strText.GetLength(), NULL);
  208. pDC->SetTextColor(oldTextColor);
  209. pDC->SetBkColor(oldBkColor);
  210. }
  211. if ((lpDIS->itemAction & ODA_FOCUS) != 0)
  212. pDC->DrawFocusRect(&rcItem);
  213. }
  214. int CXTCheckListBox::GetFontHeight()
  215. {
  216. CWindowDC dc(NULL); // get the screen device context
  217. // select the control's font into the window dc.
  218. CFont* pOldFont = dc.SelectObject(GetFont());
  219. // get the text metrics for the device context that
  220. // is using the control's font, this will give us the
  221. // height in pixels that's what we will use for the
  222. // list item's height.
  223. TEXTMETRIC tm;
  224. dc.GetTextMetrics(&tm);
  225. // restore the dc with it's original font.
  226. dc.SelectObject(pOldFont);
  227. // return the desired row height.
  228. return tm.tmHeight;
  229. }
  230. void CXTCheckListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
  231. {
  232. lpMIS->itemHeight = m_checkListState.m_sizeCheck.cy + ::GetSystemMetrics(SM_CYEDGE);
  233. }
  234. bool CXTCheckListBox::Init()
  235. {
  236. if (!CXTListBox::Init())
  237. return false;
  238. // If windows XP themes are supported, initialize the
  239. // theme wrapper used to display the check boxes.
  240. m_themeHelper.OpenThemeData(m_hWnd, L"BUTTON");
  241. return true;
  242. }
  243. bool CXTCheckListBox::PreDrawItemThemed(CDC* pDC, DRAWITEMSTRUCT &drawItem, int nCheck, int cyItem)
  244. {
  245. bool bRet = false;
  246. // Draw the check boxes using the theme API's only if the app is themed.
  247. if (m_themeHelper.IsAppThemeReady())
  248. {
  249. BOOL bEnabled = IsWindowEnabled() && IsEnabled(drawItem.itemID);
  250. int nState = 0;
  251. switch (nCheck)
  252. {
  253. case 0: // not checked
  254. nState = (bEnabled ? CBS_UNCHECKEDNORMAL : CBS_UNCHECKEDDISABLED);
  255. break;
  256. case 1: // checked
  257. nState = (bEnabled ? CBS_CHECKEDNORMAL : CBS_CHECKEDDISABLED);
  258. break;
  259. case 2: // intermediate
  260. nState = (bEnabled ? CBS_MIXEDNORMAL : CBS_MIXEDDISABLED);
  261. break;
  262. }
  263. SIZE size;
  264. if (SUCCEEDED(m_themeHelper.GetThemePartSize(pDC->m_hDC, BP_CHECKBOX, nState, NULL, TS_TRUE, &size)))
  265. {
  266. CRect rectCheck = drawItem.rcItem;
  267. rectCheck.left += 1;
  268. rectCheck.top += max(0, (cyItem - size.cy) / 2);
  269. rectCheck.right = rectCheck.left + size.cx;
  270. rectCheck.bottom = rectCheck.top + size.cy;
  271. CRect rectItem = drawItem.rcItem;
  272. rectItem.right = rectItem.left + size.cx + 2;
  273. CRect rectCheckBox = OnGetCheckPosition(rectItem, rectCheck);
  274. if (SUCCEEDED(m_themeHelper.DrawThemeBackground(pDC->m_hDC, BP_CHECKBOX, nState, &rectCheckBox, NULL)))
  275. {
  276. bRet = true;
  277. }
  278. }
  279. }
  280. return bRet;
  281. }
  282. void CXTCheckListBox::PreDrawItemNonThemed(CDC* pDC, DRAWITEMSTRUCT &drawItem, int nCheck, int cyItem)
  283. {
  284. CDC bitmapDC;
  285. if (bitmapDC.CreateCompatibleDC(pDC))
  286. {
  287. // redraw background.
  288. pDC->FillSolidRect(&drawItem.rcItem,
  289. ::GetSysColor(COLOR_WINDOW));
  290. #if _MSC_VER >= 1200 // MFC 6.0
  291. // Change Compatible DC to LTR since the bitmap is LTR
  292. DWORD dwLayoutBitmapDC = bitmapDC.GetLayout();
  293. bitmapDC.SetLayout(0);
  294. #endif
  295. HBITMAP hOldBitmap = (HBITMAP)::SelectObject(bitmapDC.m_hDC, m_checkListState.m_hbitmapCheck);
  296. CRect rectCheck = drawItem.rcItem;
  297. rectCheck.left += 1;
  298. rectCheck.top += max(0, (cyItem - m_checkListState.m_sizeCheck.cy) / 2);
  299. rectCheck.right = rectCheck.left + m_checkListState.m_sizeCheck.cx;
  300. rectCheck.bottom = rectCheck.top + m_checkListState.m_sizeCheck.cy;
  301. CRect rectItem = drawItem.rcItem;
  302. rectItem.right = rectItem.left + m_checkListState.m_sizeCheck.cx + 2;
  303. CRect rectCheckBox = OnGetCheckPosition(rectItem, rectCheck);
  304. pDC->FillSolidRect(rectItem, pDC->GetBkColor());
  305. #if _MSC_VER >= 1200 // MFC 6.0
  306. DWORD dwLayoutDC = pDC->GetLayout();
  307. // Change destination DC layout to preserve bitmap orientation
  308. pDC->SetLayout(dwLayoutDC | LAYOUT_BITMAPORIENTATIONPRESERVED);
  309. #endif
  310. pDC->BitBlt(rectCheckBox.left, rectCheckBox.top,
  311. m_checkListState.m_sizeCheck.cx, m_checkListState.m_sizeCheck.cy, &bitmapDC,
  312. m_checkListState.m_sizeCheck.cx * nCheck, 0, SRCCOPY);
  313. #if _MSC_VER >= 1200 // MFC 6.0
  314. // Restore DC layout
  315. pDC->SetLayout(dwLayoutDC);
  316. bitmapDC.SetLayout(dwLayoutBitmapDC);
  317. #endif
  318. ::SelectObject(bitmapDC.m_hDC, hOldBitmap);
  319. }
  320. }
  321. void CXTCheckListBox::PreDrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  322. {
  323. DRAWITEMSTRUCT drawItem;
  324. MEMCPY_S(&drawItem, lpDrawItemStruct, sizeof(DRAWITEMSTRUCT));
  325. if ((((LONG)drawItem.itemID) >= 0) &&
  326. ((drawItem.itemAction & (ODA_DRAWENTIRE | ODA_SELECT)) != 0))
  327. {
  328. int cyItem = GetItemHeight(drawItem.itemID);
  329. CDC* pDC = CDC::FromHandle(drawItem.hDC);
  330. COLORREF newBkColor = GetSysColor(COLOR_WINDOW);
  331. BOOL fDisabled = !IsWindowEnabled() || !IsEnabled(drawItem.itemID);
  332. if ((drawItem.itemState & ODS_SELECTED) && !fDisabled)
  333. newBkColor = GetSysColor(COLOR_HIGHLIGHT);
  334. COLORREF oldBkColor = pDC->SetBkColor(newBkColor);
  335. int nCheck = GetCheck(drawItem.itemID);
  336. if (!PreDrawItemThemed(pDC, drawItem, nCheck, cyItem))
  337. {
  338. PreDrawItemNonThemed(pDC, drawItem, nCheck, cyItem);
  339. }
  340. pDC->SetBkColor(oldBkColor);
  341. }
  342. if (drawItem.itemData != 0 && drawItem.itemData != (UINT)LB_ERR)
  343. {
  344. CHECK_DATA* pState = (CHECK_DATA*)drawItem.itemData;
  345. drawItem.itemData = pState->m_dwUserData;
  346. }
  347. drawItem.rcItem.left = drawItem.rcItem.left + m_checkListState.m_sizeCheck.cx + 2;
  348. DrawItem(&drawItem);
  349. }
  350. /*
  351. void CXTCheckListBox::PreDrawItem(LPDRAWITEMSTRUCT lpDIS)
  352. {
  353. DRAWITEMSTRUCT drawItem;
  354. MEMCPY_S(&drawItem, lpDIS, sizeof(DRAWITEMSTRUCT));
  355. if ((((LONG)drawItem.itemID) >= 0) &&
  356. ((drawItem.itemAction & (ODA_DRAWENTIRE | ODA_SELECT)) != 0))
  357. {
  358. int cyItem = GetItemHeight(drawItem.itemID);
  359. CDC* pDC = CDC::FromHandle(drawItem.hDC);
  360. COLORREF newBkColor = GetSysColor(COLOR_WINDOW);
  361. COLORREF oldBkColor = pDC->SetBkColor(newBkColor);
  362. CDC bitmapDC;
  363. if (bitmapDC.CreateCompatibleDC(pDC))
  364. {
  365. int nCheck = GetCheck(drawItem.itemID);
  366. HBITMAP hOldBitmap = (HBITMAP)::SelectObject(bitmapDC.m_hDC, m_checkListState.m_hbitmapCheck);
  367. CRect rectCheck = drawItem.rcItem;
  368. rectCheck.left += 1;
  369. rectCheck.top += 1 + __max(0, (cyItem - m_checkListState.m_sizeCheck.cy) / 2);
  370. rectCheck.right = rectCheck.left + m_checkListState.m_sizeCheck.cx;
  371. rectCheck.bottom = rectCheck.top + m_checkListState.m_sizeCheck.cy;
  372. CRect rectItem = drawItem.rcItem;
  373. rectItem.right = rectItem.left + m_checkListState.m_sizeCheck.cx + 2;
  374. CRect rectCheckBox = OnGetCheckPosition(rectItem, rectCheck);
  375. CBrush brush(newBkColor);
  376. pDC->FillRect(rectItem, &brush);
  377. pDC->BitBlt(rectCheckBox.left, rectCheckBox.top,
  378. m_checkListState.m_sizeCheck.cx, m_checkListState.m_sizeCheck.cy, &bitmapDC,
  379. m_checkListState.m_sizeCheck.cx * nCheck, 0, SRCCOPY);
  380. ::SelectObject(bitmapDC.m_hDC, hOldBitmap);
  381. brush.DeleteObject();
  382. }
  383. pDC->SetBkColor(oldBkColor);
  384. bitmapDC.DeleteDC();
  385. }
  386. if (drawItem.itemData != 0 && drawItem.itemData != (UINT)LB_ERR)
  387. {
  388. CHECK_DATA* pState = (CHECK_DATA*)drawItem.itemData;
  389. drawItem.itemData = pState->m_dwUserData;
  390. }
  391. drawItem.rcItem.left = drawItem.rcItem.left + m_checkListState.m_sizeCheck.cx + 2;
  392. DrawItem(&drawItem);
  393. }*/
  394. void CXTCheckListBox::PreMeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  395. {
  396. int cyItem = CalcMinimumItemHeight();
  397. MEASUREITEMSTRUCT measureItem;
  398. MEMCPY_S(&measureItem, lpMeasureItemStruct, sizeof(MEASUREITEMSTRUCT));
  399. measureItem.itemHeight = cyItem;
  400. measureItem.itemWidth = (UINT)-1;
  401. // WINBUG: Windows95 and Windows NT disagree on what this value
  402. // should be.  According to the docs, they are both wrong
  403. if (GetStyle() & LBS_OWNERDRAWVARIABLE)
  404. {
  405. LRESULT lResult = DefWindowProc(LB_GETITEMDATA, measureItem.itemID, 0);
  406. if (lResult != LB_ERR)
  407. measureItem.itemData = (UINT)lResult;
  408. else
  409. measureItem.itemData = 0;
  410. // WINBUG: This is only done in the LBS_OWNERDRAWVARIABLE case
  411. // because Windows 95 does not initialize itemData to zero in the
  412. // case of LBS_OWNERDRAWFIXED list boxes (it is stack garbage).
  413. if (measureItem.itemData != 0 && measureItem.itemData != (UINT)LB_ERR)
  414. {
  415. CHECK_DATA* pState = (CHECK_DATA*)measureItem.itemData;
  416. measureItem.itemData = pState->m_dwUserData;
  417. }
  418. }
  419. MeasureItem(&measureItem);
  420. lpMeasureItemStruct->itemHeight = max(measureItem.itemHeight, (UINT) cyItem);
  421. lpMeasureItemStruct->itemWidth = measureItem.itemWidth;
  422. }
  423. int CXTCheckListBox::PreCompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
  424. {
  425. COMPAREITEMSTRUCT compareItem;
  426. MEMCPY_S(&compareItem, lpCompareItemStruct, sizeof(COMPAREITEMSTRUCT));
  427. if (compareItem.itemData1 != 0 && compareItem.itemData1 != (UINT)LB_ERR)
  428. {
  429. CHECK_DATA* pState = (CHECK_DATA*)compareItem.itemData1;
  430. compareItem.itemData1 = pState->m_dwUserData;
  431. }
  432. if (compareItem.itemData2 != 0 && compareItem.itemData2 != (UINT)LB_ERR)
  433. {
  434. CHECK_DATA* pState = (CHECK_DATA*)compareItem.itemData2;
  435. compareItem.itemData2 = pState->m_dwUserData;
  436. }
  437. return CompareItem(&compareItem);
  438. }
  439. void CXTCheckListBox::PreDeleteItem(LPDELETEITEMSTRUCT lpDeleteItemStruct)
  440. {
  441. DELETEITEMSTRUCT deleteItem;
  442. MEMCPY_S(&deleteItem, lpDeleteItemStruct, sizeof(DELETEITEMSTRUCT));
  443. // WINBUG: The following if block is required because Windows NT
  444. // version 3.51 does not properly fill out the LPDELETEITEMSTRUCT.
  445. if (deleteItem.itemData == 0)
  446. {
  447. LRESULT lResult = DefWindowProc(LB_GETITEMDATA, deleteItem.itemID, 0);
  448. if (lResult != LB_ERR)
  449. deleteItem.itemData = (UINT)lResult;
  450. }
  451. if (deleteItem.itemData != 0 && deleteItem.itemData != (UINT)LB_ERR)
  452. {
  453. CHECK_DATA* pState = (CHECK_DATA*)deleteItem.itemData;
  454. deleteItem.itemData = pState->m_dwUserData;
  455. delete pState;
  456. }
  457. DeleteItem(&deleteItem);
  458. }
  459. BOOL CXTCheckListBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  460. {
  461. switch (message)
  462. {
  463. case WM_DRAWITEM:
  464. ASSERT(pResult == NULL);       // no return value expected
  465. PreDrawItem((LPDRAWITEMSTRUCT)lParam);
  466. break;
  467. case WM_MEASUREITEM:
  468. ASSERT(pResult == NULL);       // no return value expected
  469. PreMeasureItem((LPMEASUREITEMSTRUCT)lParam);
  470. break;
  471. case WM_COMPAREITEM:
  472. ASSERT(pResult != NULL);       // return value expected
  473. if (pResult == NULL)
  474. break;
  475. *pResult = PreCompareItem((LPCOMPAREITEMSTRUCT)lParam);
  476. break;
  477. case WM_DELETEITEM:
  478. ASSERT(pResult == NULL);       // no return value expected
  479. PreDeleteItem((LPDELETEITEMSTRUCT)lParam);
  480. break;
  481. default:
  482. return CXTListBox::OnChildNotify(message, wParam, lParam, pResult);
  483. }
  484. return TRUE;
  485. }
  486. void CXTCheckListBox::PreSubclassWindow()
  487. {
  488. CXTListBox::PreSubclassWindow();
  489. #ifdef _DEBUG
  490. // CCheckListBoxes must be owner drawn
  491. ASSERT(GetStyle() & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE));
  492. #endif
  493. }
  494. int CXTCheckListBox::CalcMinimumItemHeight()
  495. {
  496. int nResult;
  497. if ((GetStyle() & (LBS_HASSTRINGS | LBS_OWNERDRAWFIXED)) ==
  498. (LBS_HASSTRINGS | LBS_OWNERDRAWFIXED))
  499. {
  500. CClientDC dc(this);
  501. CFont* pOldFont = dc.SelectObject(GetFont());
  502. TEXTMETRIC tm;
  503. VERIFY (dc.GetTextMetrics (&tm));
  504. dc.SelectObject(pOldFont);
  505. m_cyText = tm.tmHeight;
  506. nResult = max(m_checkListState.m_sizeCheck.cy + 1, m_cyText);
  507. }
  508. else
  509. {
  510. nResult = m_checkListState.m_sizeCheck.cy + 1;
  511. }
  512. return nResult;
  513. }
  514. void CXTCheckListBox::InvalidateCheck(int nIndex)
  515. {
  516. CRect rect;
  517. GetItemRect(nIndex, rect);
  518. rect.right = rect.left + m_checkListState.m_sizeCheck.cx + 2;
  519. InvalidateRect(rect, FALSE);
  520. }
  521. void CXTCheckListBox::InvalidateItem(int nIndex)
  522. {
  523. CRect rect;
  524. GetItemRect(nIndex, rect);
  525. InvalidateRect(rect, FALSE);
  526. }
  527. int CXTCheckListBox::CheckFromPoint(CPoint point, BOOL& bInCheck)
  528. {
  529. // assume did not hit anything
  530. bInCheck = FALSE;
  531. int nIndex = -1;
  532. if ((GetStyle() & (LBS_OWNERDRAWFIXED | LBS_MULTICOLUMN)) == LBS_OWNERDRAWFIXED)
  533. {
  534. // optimized case for ownerdraw fixed, single column
  535. int cyItem = GetItemHeight(0);
  536. if (point.y < cyItem * GetCount())
  537. {
  538. nIndex = GetTopIndex() + point.y / cyItem;
  539. if (point.x < m_checkListState.m_sizeCheck.cx + 2)
  540. ++bInCheck;
  541. }
  542. }
  543. else
  544. {
  545. // general case for ownerdraw variable or multiple column
  546. int i;
  547. for (i = GetTopIndex(); i < GetCount(); i++)
  548. {
  549. CRect itemRect;
  550. GetItemRect(i, &itemRect);
  551. if (itemRect.PtInRect(point))
  552. {
  553. nIndex = i;
  554. if (point.x < itemRect.left + m_checkListState.m_sizeCheck.cx + 2)
  555. ++bInCheck;
  556. break;
  557. }
  558. }
  559. }
  560. return nIndex;
  561. }
  562. void CXTCheckListBox::SetSelectionCheck(int nCheck)
  563. {
  564. int iSelectedItem, nSelectedItems = GetSelCount();
  565. if (nSelectedItems > 0)
  566. {
  567. CArray<int, int&> rgiSelectedItems;
  568. rgiSelectedItems.SetSize(nSelectedItems);
  569. int* piSelectedItems = rgiSelectedItems.GetData();
  570. GetSelItems(nSelectedItems, piSelectedItems);
  571. for (iSelectedItem = 0; iSelectedItem < nSelectedItems; iSelectedItem++)
  572. {
  573. if (IsEnabled(piSelectedItems[iSelectedItem]))
  574. {
  575. SetCheck(piSelectedItems[iSelectedItem], nCheck);
  576. InvalidateCheck(piSelectedItems[iSelectedItem]);
  577. }
  578. }
  579. }
  580. }
  581. void CXTCheckListBox::OnLButtonDown(UINT nFlags, CPoint point)
  582. {
  583. CRect itemRect;
  584. CRect clientRect;
  585. GetClientRect(clientRect);
  586. int nIndex;
  587. for (nIndex = GetTopIndex(); nIndex < GetCount(); nIndex++)
  588. {
  589. GetItemRect(nIndex, &itemRect);
  590. if (!clientRect.PtInRect(itemRect.TopLeft()))
  591. break;
  592. if (itemRect.PtInRect(point) && IsEnabled(nIndex))
  593. {
  594. if (m_nStyle != BS_CHECKBOX && m_nStyle != BS_3STATE)
  595. {
  596. if (point.x - itemRect.left < m_checkListState.m_sizeCheck.cx + 2)
  597. {
  598. CWnd* pParent = GetParent();
  599. ASSERT_VALID(pParent);
  600. int nModulo = (m_nStyle == BS_AUTO3STATE) ? 3 : 2;
  601. int nCheck = GetCheck(nIndex);
  602. nCheck = (nCheck == nModulo) ? nCheck - 1 : nCheck;
  603. SetCheck(nIndex, (nCheck + 1) % nModulo);
  604. InvalidateCheck(nIndex);
  605. CXTListBox::OnLButtonDown(nFlags, point);
  606. // Inform of check
  607. pParent->SendMessage(WM_COMMAND,
  608. MAKEWPARAM(GetDlgCtrlID(), CLBN_CHKCHANGE),
  609. (LPARAM)m_hWnd);
  610. return;
  611. }
  612. }
  613. else
  614. return; // Swallow LButtons for disabled items
  615. }
  616. }
  617. // do default listbox selection logic
  618. CXTListBox::OnLButtonDown(nFlags, point);
  619. }
  620. void CXTCheckListBox::OnLButtonDblClk(UINT nFlags, CPoint point)
  621. {
  622. CRect itemRect;
  623. CRect clientRect;
  624. GetClientRect(clientRect);
  625. int nIndex;
  626. for (nIndex = GetTopIndex(); nIndex < GetCount(); nIndex++)
  627. {
  628. GetItemRect(nIndex, &itemRect);
  629. if (!clientRect.PtInRect(itemRect.TopLeft()))
  630. break;
  631. if (itemRect.PtInRect(point) && IsEnabled(nIndex))
  632. {
  633. if (m_nStyle != BS_CHECKBOX && m_nStyle != BS_3STATE)
  634. {
  635. CWnd* pParent = GetParent();
  636. ASSERT_VALID(pParent);
  637. int nModulo = (m_nStyle == BS_AUTO3STATE) ? 3 : 2;
  638. int nCheck = GetCheck(nIndex);
  639. nCheck = (nCheck == nModulo) ? nCheck - 1 : nCheck;
  640. SetCheck(nIndex, (nCheck + 1) % nModulo);
  641. InvalidateCheck(nIndex);
  642. CXTListBox::OnLButtonDown(nFlags, point);
  643. // Inform of check
  644. pParent->SendMessage(WM_COMMAND,
  645. MAKEWPARAM(GetDlgCtrlID(), CLBN_CHKCHANGE),
  646. (LPARAM)m_hWnd);
  647. return;
  648. }
  649. else
  650. return; // Swallow LButtons for disabled items
  651. }
  652. }
  653. CXTListBox::OnLButtonDblClk(nFlags, point);
  654. }
  655. void CXTCheckListBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  656. {
  657. if (nChar == VK_SPACE)
  658. {
  659. int nIndex = GetCaretIndex();
  660. CWnd* pParent = GetParent();
  661. ASSERT_VALID(pParent);
  662. if (nIndex != LB_ERR)
  663. {
  664. if (m_nStyle != BS_CHECKBOX && m_nStyle != BS_3STATE)
  665. {
  666. if ((GetStyle() & LBS_MULTIPLESEL) != 0)
  667. {
  668. if (IsEnabled(nIndex))
  669. {
  670. BOOL bSelected = GetSel(nIndex);
  671. if (bSelected)
  672. {
  673. int nModulo = (m_nStyle == BS_AUTO3STATE) ? 3 : 2;
  674. int nCheck = GetCheck(nIndex);
  675. nCheck = (nCheck == nModulo) ? nCheck - 1 : nCheck;
  676. SetCheck(nIndex, (nCheck + 1) % nModulo);
  677. // Inform of check
  678. pParent->SendMessage(WM_COMMAND,
  679. MAKEWPARAM(GetDlgCtrlID(), CLBN_CHKCHANGE),
  680. (LPARAM)m_hWnd);
  681. }
  682. SetSel(nIndex, !bSelected);
  683. }
  684. else
  685. SetSel(nIndex, FALSE); // unselect disabled items
  686. return;
  687. }
  688. else
  689. {
  690. // If there is a selection, the space bar toggles that check,
  691. // all other keys are the same as a standard listbox.
  692. if (IsEnabled(nIndex))
  693. {
  694. int nModulo = (m_nStyle == BS_AUTO3STATE) ? 3 : 2;
  695. int nCheck = GetCheck(nIndex);
  696. nCheck = (nCheck == nModulo) ? nCheck - 1 : nCheck;
  697. int nNewCheck = (nCheck + 1)%nModulo;
  698. SetCheck(nIndex, nNewCheck);
  699. InvalidateCheck(nIndex);
  700. if (GetStyle() & LBS_EXTENDEDSEL)
  701. {
  702. // The listbox is a multi-select listbox, and the user
  703. // clicked on a selected check, so change the check on all
  704. // of the selected items.
  705. SetSelectionCheck(nNewCheck);
  706. }
  707. // Inform of check
  708. pParent->SendMessage(WM_COMMAND,
  709. MAKEWPARAM(GetDlgCtrlID(), CLBN_CHKCHANGE),
  710. (LPARAM)m_hWnd);
  711. }
  712. else
  713. SetSel(nIndex, FALSE); // unselect disabled items
  714. return;
  715. }
  716. }
  717. }
  718. }
  719. CXTListBox::OnKeyDown(nChar, nRepCnt, nFlags);
  720. }
  721. int CXTCheckListBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
  722. {
  723. if (CXTListBox::OnCreate(lpCreateStruct) == -1)
  724. return -1;
  725. if ((GetStyle() & (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS)) ==
  726. (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS))
  727. {
  728. SetItemHeight(0, CalcMinimumItemHeight());
  729. }
  730. return 0;
  731. }
  732. LRESULT CXTCheckListBox::OnSetFont(WPARAM , LPARAM)
  733. {
  734. Default();
  735. if ((GetStyle() & (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS)) ==
  736. (LBS_OWNERDRAWFIXED | LBS_HASSTRINGS))
  737. {
  738. SetItemHeight(0, CalcMinimumItemHeight());
  739. }
  740. return 0;
  741. }
  742. LRESULT CXTCheckListBox::OnLBAddString(WPARAM wParam, LPARAM lParam)
  743. {
  744. CHECK_DATA* pState = NULL;
  745. if (!(GetStyle() & LBS_HASSTRINGS))
  746. {
  747. pState = new CHECK_DATA;
  748. pState->m_dwUserData = lParam;
  749. lParam = (LPARAM)pState;
  750. }
  751. LRESULT lResult = DefWindowProc(LB_ADDSTRING, wParam, lParam);
  752. if (lResult == LB_ERR && pState != NULL)
  753. delete pState;
  754. return lResult;
  755. }
  756. LRESULT CXTCheckListBox::OnLBFindString(WPARAM wParam, LPARAM lParam)
  757. {
  758. if (GetStyle() & LBS_HASSTRINGS)
  759. return DefWindowProc(LB_FINDSTRING, wParam, lParam);
  760. int nIndex = (int)wParam;
  761. if (nIndex == -1) nIndex = 0;
  762. for (; nIndex < GetCount(); nIndex++)
  763. if ((UINT_PTR)lParam == GetItemData(nIndex))
  764. return nIndex;
  765. return LB_ERR;
  766. }
  767. LRESULT CXTCheckListBox::OnLBFindStringExact(WPARAM wParam, LPARAM lParam)
  768. {
  769. if (GetStyle() & (LBS_HASSTRINGS | LBS_SORT))
  770. return DefWindowProc(LB_FINDSTRINGEXACT, wParam, lParam);
  771. int nIndex = (int)wParam;
  772. if (nIndex == -1) nIndex = 0;
  773. for (; nIndex < GetCount(); nIndex++)
  774. if ((UINT_PTR)lParam == GetItemData(nIndex))
  775. return nIndex;
  776. return LB_ERR;
  777. }
  778. LRESULT CXTCheckListBox::OnLBGetItemData(WPARAM wParam, LPARAM lParam)
  779. {
  780. LRESULT lResult = DefWindowProc(LB_GETITEMDATA, wParam, lParam);
  781. if (lResult != LB_ERR)
  782. {
  783. CHECK_DATA* pState = (CHECK_DATA*)lResult;
  784. if (pState == NULL)
  785. return 0; // default
  786. lResult = pState->m_dwUserData;
  787. }
  788. return lResult;
  789. }
  790. LRESULT CXTCheckListBox::OnLBGetText(WPARAM wParam, LPARAM lParam)
  791. {
  792. LRESULT lResult = DefWindowProc(LB_GETTEXT, wParam, lParam);
  793. if (GetStyle() & LBS_HASSTRINGS)
  794. return lResult;
  795. if (lResult != LB_ERR)
  796. {
  797. CHECK_DATA* pState = (CHECK_DATA*)lParam;
  798. if (pState != NULL)
  799. lParam = pState->m_dwUserData;
  800. }
  801. return lResult;
  802. }
  803. LRESULT CXTCheckListBox::OnLBInsertString(WPARAM wParam, LPARAM lParam)
  804. {
  805. CHECK_DATA* pState = NULL;
  806. if (!(GetStyle() & LBS_HASSTRINGS))
  807. {
  808. pState = new CHECK_DATA;
  809. pState->m_dwUserData = lParam;
  810. lParam = (LPARAM)pState;
  811. }
  812. LRESULT lResult = DefWindowProc(LB_INSERTSTRING, wParam, lParam);
  813. if (lResult == LB_ERR && pState != NULL)
  814. delete pState;
  815. return lResult;
  816. }
  817. LRESULT CXTCheckListBox::OnLBSelectString(WPARAM wParam, LPARAM lParam)
  818. {
  819. if (GetStyle() & LBS_HASSTRINGS)
  820. return DefWindowProc(LB_SELECTSTRING, wParam, lParam);
  821. int nIndex = (int)wParam;
  822. if (nIndex == -1) nIndex = 0;
  823. for (; nIndex < GetCount(); nIndex++)
  824. if ((UINT_PTR)lParam == GetItemData(nIndex))
  825. {
  826. SetCurSel(nIndex);
  827. return nIndex;
  828. }
  829. return LB_ERR;
  830. }
  831. LRESULT CXTCheckListBox::OnLBSetItemData(WPARAM wParam, LPARAM lParam)
  832. {
  833. LRESULT lResult = DefWindowProc(LB_GETITEMDATA, wParam, 0);
  834. if (lResult != LB_ERR)
  835. {
  836. CHECK_DATA* pState = (CHECK_DATA*)lResult;
  837. if (pState == NULL)
  838. pState = new CHECK_DATA;
  839. pState->m_dwUserData = lParam;
  840. lResult = DefWindowProc(LB_SETITEMDATA, wParam, (LPARAM)pState);
  841. if (lResult == LB_ERR)
  842. delete pState;
  843. }
  844. return lResult;
  845. }
  846. LRESULT CXTCheckListBox::OnLBSetItemHeight(WPARAM wParam, LPARAM lParam)
  847. {
  848. int nHeight = max(CalcMinimumItemHeight(), (int)LOWORD(lParam));
  849. return DefWindowProc(LB_SETITEMHEIGHT, wParam, MAKELPARAM(nHeight, 0));
  850. }