Leaked source code of windows server 2003
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.

302 lines
6.7 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. // CMyListCtrl
  19. IMPLEMENT_DYNCREATE(CMyListCtrl, CListCtrl)
  20. BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
  21. //{{AFX_MSG_MAP(CMyListCtrl)
  22. ON_WM_LBUTTONDOWN()
  23. ON_WM_CHAR()
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CMyListCtrl construction/destruction
  28. CMyListCtrl::CMyListCtrl()
  29. {
  30. m_bFullRowSel = TRUE;
  31. }
  32. CMyListCtrl::~CMyListCtrl()
  33. {
  34. }
  35. BOOL CMyListCtrl::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);
  40. m_bFullRowSel = TRUE;
  41. return(CListCtrl::PreCreateWindow(cs));
  42. }
  43. BOOL CMyListCtrl::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 CMyListCtrl::GetFullRowSel()
  57. {
  58. return(m_bFullRowSel);
  59. }
  60. BOOL CMyListCtrl::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 CMyListCtrl::IsSelected(int nItemIndex)
  71. {
  72. return GetItemState(nItemIndex, LVIS_SELECTED);
  73. }
  74. BOOL CMyListCtrl::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 CMyListCtrl::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 CMyListCtrl::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 CMyListCtrl::AddItem
  99. (
  100. LPCTSTR pName,
  101. LPCTSTR pType,
  102. LPCTSTR pComment,
  103. UINT uState
  104. )
  105. {
  106. // insert items
  107. LV_ITEM lvi;
  108. int nItem;
  109. lvi.mask = LVIF_TEXT | LVIF_STATE;
  110. lvi.iItem = GetItemCount();
  111. lvi.iSubItem = 0;
  112. lvi.pszText = (LPTSTR) pName;
  113. lvi.iImage = 0;
  114. lvi.stateMask = LVIS_STATEIMAGEMASK;
  115. lvi.state = INDEXTOSTATEIMAGEMASK(uState);
  116. nItem = InsertItem(&lvi);
  117. SetItemText(nItem, 1, pType);
  118. SetItemText(nItem, 2, pComment);
  119. return nItem;
  120. }
  121. int CMyListCtrl::AddItem
  122. (
  123. LPCTSTR pName,
  124. LPCTSTR pComment,
  125. UINT uState
  126. )
  127. {
  128. // insert items
  129. LV_ITEM lvi;
  130. int nItem;
  131. lvi.mask = LVIF_TEXT | LVIF_STATE;
  132. lvi.iItem = GetItemCount();
  133. lvi.iSubItem = 0;
  134. lvi.pszText = (LPTSTR) pName;
  135. lvi.iImage = 0;
  136. lvi.stateMask = LVIS_STATEIMAGEMASK;
  137. lvi.state = INDEXTOSTATEIMAGEMASK(uState);
  138. nItem = InsertItem(&lvi);
  139. SetItemText(nItem, 1, pComment);
  140. return nItem;
  141. }
  142. int CMyListCtrl::GetSelectedItem()
  143. {
  144. // NOTE: This list object assumes single selection and will return the
  145. // first selection in the list. Returns -1 for nothing selected.
  146. int nSelectedItem = -1;
  147. for (int i = 0; i < GetItemCount(); i++)
  148. {
  149. UINT uState = GetItemState(i, LVIS_SELECTED);
  150. if (uState)
  151. {
  152. // item is selected
  153. nSelectedItem = i;
  154. break;
  155. }
  156. }
  157. return nSelectedItem;
  158. }
  159. void CMyListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  160. {
  161. UINT uFlags = 0;
  162. int nHitItem = HitTest(point, &uFlags);
  163. // only check the item if the user clicks on the state icon.
  164. // if the user clicks outside the text and icon, we get
  165. // a LVHT_ONITEM message which is:
  166. // LVHT_ONITEMSTATEICON | LVHT_ONITEMICON | LVHT_ONITEMLABEL
  167. // so we need to filter out the state icon hit
  168. BOOL bHit = FALSE;
  169. if ((uFlags & LVHT_ONITEMSTATEICON) &&
  170. !((uFlags & LVHT_ONITEMICON) ||
  171. (uFlags & LVHT_ONITEMLABEL)) )
  172. {
  173. bHit = TRUE;
  174. }
  175. if (bHit)
  176. CheckItem(nHitItem);
  177. else
  178. CListCtrl::OnLButtonDown(nFlags, point);
  179. }
  180. //----------------------------------------------------------------------------
  181. // Function: CListCtrlEx::OnChar
  182. //
  183. // Handles the 'WM_CHAR' message for the list-control.
  184. // This allows users to change items' checked-states using the keyboard.
  185. //----------------------------------------------------------------------------
  186. VOID
  187. CMyListCtrl::OnChar(
  188. UINT nChar,
  189. UINT nRepCnt,
  190. UINT nFlags
  191. )
  192. {
  193. BOOL fSet = FALSE;
  194. BOOL fClear = FALSE;
  195. BOOL fToggle = FALSE;
  196. INT iItem;
  197. //
  198. // Handle characters with special meaning for us
  199. //
  200. switch (nChar) {
  201. case TEXT(' '): { fToggle = TRUE; break; }
  202. case TEXT('+'):
  203. case TEXT('='): { fSet = TRUE; break; }
  204. case TEXT('-'): { fClear = TRUE; break; }
  205. }
  206. if (!fToggle && !fSet && !fClear)
  207. {
  208. CListCtrl::OnChar(nChar, nRepCnt, nFlags);
  209. }
  210. else
  211. {
  212. //
  213. // Change the state of all the selected items
  214. //
  215. for (iItem = GetNextItem(-1, LVNI_SELECTED);
  216. iItem != -1;
  217. iItem = GetNextItem(iItem, LVNI_SELECTED))
  218. {
  219. if (fToggle)
  220. {
  221. SetCheck(iItem, !GetCheck(iItem));
  222. }
  223. else
  224. if (fSet)
  225. {
  226. if (!GetCheck(iItem))
  227. {
  228. SetCheck(iItem, TRUE);
  229. }
  230. }
  231. else
  232. {
  233. if (GetCheck(iItem))
  234. {
  235. SetCheck(iItem, FALSE);
  236. }
  237. }
  238. }
  239. }
  240. }