Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
5.9 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1998 **/
  4. /**********************************************************************/
  5. /*
  6. listview.cpp
  7. Individual option property page
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include "ListView.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CListCtrlExt
  19. IMPLEMENT_DYNCREATE(CListCtrlExt, CListCtrl)
  20. BEGIN_MESSAGE_MAP(CListCtrlExt, CListCtrl)
  21. //{{AFX_MSG_MAP(CListCtrlExt)
  22. ON_WM_LBUTTONDOWN()
  23. ON_WM_CHAR()
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CListCtrlExt construction/destruction
  28. CListCtrlExt::CListCtrlExt()
  29. {
  30. m_bFullRowSel = TRUE;
  31. }
  32. CListCtrlExt::~CListCtrlExt()
  33. {
  34. }
  35. BOOL CListCtrlExt::PreCreateWindow(CREATESTRUCT& cs)
  36. {
  37. // default is report view and full row selection
  38. cs.style &= ~LVS_TYPEMASK;
  39. cs.style |= (LVS_REPORT | LVS_SHAREIMAGELISTS | LVS_SINGLESEL | LVS_SHOWSELALWAYS | LVS_NOCOLUMNHEADER);
  40. m_bFullRowSel = TRUE;
  41. return(CListCtrl::PreCreateWindow(cs));
  42. }
  43. BOOL CListCtrlExt::SetFullRowSel(BOOL bFullRowSel)
  44. {
  45. // full row selection is the only extended style this
  46. // class supports...
  47. BOOL bRet = FALSE;
  48. if (!m_hWnd)
  49. return bRet;
  50. if (bFullRowSel)
  51. bRet = ListView_SetExtendedListViewStyle(m_hWnd, LVS_EX_FULLROWSELECT);
  52. else
  53. bRet = ListView_SetExtendedListViewStyle(m_hWnd, 0);
  54. return(bRet);
  55. }
  56. BOOL CListCtrlExt::GetFullRowSel()
  57. {
  58. return(m_bFullRowSel);
  59. }
  60. BOOL CListCtrlExt::SelectItem(int nItemIndex)
  61. {
  62. LV_ITEM lvItem;
  63. ZeroMemory(&lvItem, sizeof(lvItem));
  64. lvItem.iItem = nItemIndex;
  65. lvItem.mask = LVIF_STATE;
  66. lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  67. lvItem.state = LVIS_SELECTED | LVIS_FOCUSED;
  68. return SetItem(&lvItem);
  69. }
  70. BOOL CListCtrlExt::IsSelected(int nItemIndex)
  71. {
  72. return GetItemState(nItemIndex, LVIS_SELECTED);
  73. }
  74. BOOL CListCtrlExt::CheckItem(int nItemIndex)
  75. {
  76. // this just toggles the check mark state
  77. UINT uState = GetItemState(nItemIndex, LVIS_STATEIMAGEMASK);
  78. UINT uCheckMask = INDEXTOSTATEIMAGEMASK(LISTVIEWEX_CHECKED);
  79. uState = (uState == uCheckMask) ? LISTVIEWEX_NOT_CHECKED : LISTVIEWEX_CHECKED;
  80. return SetItemState(nItemIndex,
  81. INDEXTOSTATEIMAGEMASK(uState), LVIS_STATEIMAGEMASK);
  82. }
  83. BOOL CListCtrlExt::SetCheck(int nItemIndex, BOOL fCheck)
  84. {
  85. // this just toggles the check mark state
  86. UINT uState;
  87. uState = (fCheck) ? LISTVIEWEX_CHECKED : LISTVIEWEX_NOT_CHECKED;
  88. return SetItemState(nItemIndex,
  89. INDEXTOSTATEIMAGEMASK(uState), LVIS_STATEIMAGEMASK);
  90. }
  91. UINT CListCtrlExt::GetCheck(int nItemIndex)
  92. {
  93. // return 1 for checked item, 0 for unchecked
  94. UINT uState = GetItemState(nItemIndex, LVIS_STATEIMAGEMASK);
  95. UINT uCheckMask = INDEXTOSTATEIMAGEMASK(LISTVIEWEX_CHECKED);
  96. return uState == uCheckMask;
  97. }
  98. int CListCtrlExt::AddItem
  99. (
  100. LPCTSTR pText,
  101. int nRow
  102. )
  103. {
  104. // insert items
  105. LV_ITEM lvi = {0};
  106. lvi.mask = LVIF_TEXT | LVIF_STATE;
  107. //lvi.iItem = GetItemCount();
  108. lvi.iItem = nRow;
  109. lvi.iSubItem = 0;
  110. lvi.pszText = (LPTSTR) pText;
  111. lvi.iImage = 0;
  112. lvi.stateMask = LVIS_STATEIMAGEMASK;
  113. lvi.state = INDEXTOSTATEIMAGEMASK(LISTVIEWEX_NOT_CHECKED);
  114. return InsertItem(&lvi);
  115. }
  116. int CListCtrlExt::GetSelectedItem()
  117. {
  118. // NOTE: This list object assumes single selection and will return the
  119. // first selection in the list. Returns -1 for nothing selected.
  120. int nSelectedItem = -1;
  121. for (int i = 0; i < GetItemCount(); i++)
  122. {
  123. UINT uState = GetItemState(i, LVIS_SELECTED);
  124. if (uState)
  125. {
  126. // item is selected
  127. nSelectedItem = i;
  128. break;
  129. }
  130. }
  131. return nSelectedItem;
  132. }
  133. void CListCtrlExt::OnLButtonDown(UINT nFlags, CPoint point)
  134. {
  135. UINT uFlags = 0;
  136. int nHitItem = HitTest(point, &uFlags);
  137. // only check the item if the user clicks on the state icon.
  138. // if the user clicks outside the text and icon, we get
  139. // a LVHT_ONITEM message which is:
  140. // LVHT_ONITEMSTATEICON | LVHT_ONITEMICON | LVHT_ONITEMLABEL
  141. // so we need to filter out the state icon hit
  142. BOOL bHit = FALSE;
  143. if ((uFlags & LVHT_ONITEMSTATEICON) &&
  144. !((uFlags & LVHT_ONITEMICON) ||
  145. (uFlags & LVHT_ONITEMLABEL)) )
  146. {
  147. bHit = TRUE;
  148. }
  149. if (bHit)
  150. CheckItem(nHitItem);
  151. else
  152. CListCtrl::OnLButtonDown(nFlags, point);
  153. }
  154. //----------------------------------------------------------------------------
  155. // Function: CListCtrlExt::OnChar
  156. //
  157. // Handles the 'WM_CHAR' message for the list-control.
  158. // This allows users to change items' checked-states using the keyboard.
  159. //----------------------------------------------------------------------------
  160. VOID
  161. CListCtrlExt::OnChar(
  162. UINT nChar,
  163. UINT nRepCnt,
  164. UINT nFlags
  165. )
  166. {
  167. BOOL fSet = FALSE;
  168. BOOL fClear = FALSE;
  169. BOOL fToggle = FALSE;
  170. INT iItem;
  171. //
  172. // Handle characters with special meaning for us
  173. //
  174. switch (nChar) {
  175. case TEXT(' '): { fToggle = TRUE; break; }
  176. case TEXT('+'):
  177. case TEXT('='): { fSet = TRUE; break; }
  178. case TEXT('-'): { fClear = TRUE; break; }
  179. }
  180. if (!fToggle && !fSet && !fClear)
  181. {
  182. CListCtrl::OnChar(nChar, nRepCnt, nFlags);
  183. }
  184. else
  185. {
  186. //
  187. // Change the state of all the selected items
  188. //
  189. for (iItem = GetNextItem(-1, LVNI_SELECTED);
  190. iItem != -1;
  191. iItem = GetNextItem(iItem, LVNI_SELECTED))
  192. {
  193. if (fToggle)
  194. {
  195. SetCheck(iItem, !GetCheck(iItem));
  196. }
  197. else
  198. if (fSet)
  199. {
  200. if (!GetCheck(iItem))
  201. {
  202. SetCheck(iItem, TRUE);
  203. }
  204. }
  205. else
  206. {
  207. if (GetCheck(iItem))
  208. {
  209. SetCheck(iItem, FALSE);
  210. }
  211. }
  212. }
  213. }
  214. }