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.

250 lines
5.9 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1991-1996 **/
  4. /**********************************************************************/
  5. /*
  6. UserList.cpp
  7. CListBox class for owner draw list that displays users and groups
  8. FILE HISTORY:
  9. jony Apr-1996 created
  10. */
  11. #include "stdafx.h"
  12. #include "resource.h"
  13. #include "UserList.h"
  14. const unsigned short BITMAP_HEIGHT = 18;
  15. const unsigned short BITMAP_WIDTH = 18;
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CUserList
  23. CUserList::CUserList()
  24. {
  25. m_pBitmap[0] = new CTransBmp;
  26. m_pBitmap[0]->LoadBitmap(IDB_USER_BITMAP);
  27. m_pBitmap[1] = new CTransBmp;
  28. m_pBitmap[1]->LoadBitmap(IDB_GLOBAL_GROUP_BITMAP);
  29. m_pBitmap[2] = new CTransBmp;
  30. m_pBitmap[2]->LoadBitmap(IDB_WORLD);
  31. m_pBitmap[3] = new CTransBmp;
  32. m_pBitmap[3]->LoadBitmap(IDB_LOCAL_GROUP_BITMAP);
  33. m_pBitmap[4] = new CTransBmp;
  34. m_pBitmap[4]->LoadBitmap(IDB_LUSER_BITMAP);
  35. m_sHScrollWidth = 0;
  36. }
  37. CUserList::~CUserList()
  38. {
  39. delete m_pBitmap[0];
  40. delete m_pBitmap[1];
  41. delete m_pBitmap[2];
  42. delete m_pBitmap[3];
  43. delete m_pBitmap[4];
  44. }
  45. BEGIN_MESSAGE_MAP(CUserList, CListBox)
  46. //{{AFX_MSG_MAP(CUserList)
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CUserList message handlers
  51. void CUserList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  52. {
  53. // is this a valid item?
  54. if ((GetCount() == LB_ERR) || (lpDrawItemStruct->itemID > (UINT)GetCount())) return;
  55. COLORREF crefOldText;
  56. COLORREF crefOldBk;
  57. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  58. pDC->SetBkMode(TRANSPARENT);
  59. HBRUSH hBrush;
  60. CTransBmp* pTempBmp = (CTransBmp*)lpDrawItemStruct->itemData;
  61. switch (lpDrawItemStruct->itemAction)
  62. {
  63. case ODA_SELECT:
  64. case ODA_DRAWENTIRE:
  65. // Display the text associated with the item.
  66. HBITMAP hBitmapOld;
  67. // Is the item selected?
  68. if (lpDrawItemStruct->itemState & ODS_SELECTED)
  69. {
  70. hBrush = CreateSolidBrush( GetSysColor(COLOR_HIGHLIGHT));
  71. hBitmapOld = (HBITMAP)pDC->SelectObject(pTempBmp);
  72. crefOldText = pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT) );
  73. crefOldBk = pDC->SetBkColor(GetSysColor(COLOR_HIGHLIGHT) );
  74. }
  75. else
  76. {
  77. hBrush = (HBRUSH)GetStockObject( GetSysColor(COLOR_WINDOW));
  78. hBitmapOld = (HBITMAP)pDC->SelectObject(pTempBmp);
  79. crefOldText = pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
  80. crefOldBk = pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
  81. }
  82. pDC->FillRect(&(lpDrawItemStruct->rcItem), CBrush::FromHandle(hBrush));
  83. // display text
  84. // split apart the string to put the comments out to the side
  85. TCHAR* pName = (TCHAR*)malloc(255 * sizeof(TCHAR));
  86. if (pName == NULL)
  87. {
  88. AfxMessageBox(IDS_GENERIC_NO_HEAP, MB_ICONEXCLAMATION);
  89. exit(1);
  90. }
  91. GetText(lpDrawItemStruct->itemID, pName);
  92. TCHAR* pName2;
  93. pName = _tcstok(pName, _T(";")); // gets the name
  94. pName2 = _tcstok(NULL, _T(";")); // gets the comment
  95. // format the name + comment
  96. int nTop = (lpDrawItemStruct->rcItem.bottom + lpDrawItemStruct->rcItem.top) / 2;
  97. pDC->TextOut(BITMAP_WIDTH + 6,
  98. (nTop - 8),
  99. pName);
  100. if (pName2 != NULL) pDC->TextOut(200,
  101. (nTop - 8),
  102. pName2);
  103. // calculate width for horizontal scrolling
  104. CSize cs;
  105. cs = pDC->GetTextExtent(pName2);
  106. short nWidth = cs.cx + 200;
  107. if (nWidth > m_sHScrollWidth)
  108. {
  109. m_sHScrollWidth = nWidth;
  110. SetHorizontalExtent(m_sHScrollWidth);
  111. }
  112. free(pName);
  113. // Display bitmap
  114. nTop = (lpDrawItemStruct->rcItem.bottom + lpDrawItemStruct->rcItem.top - BITMAP_HEIGHT) / 2;
  115. pTempBmp->DrawTrans(pDC, lpDrawItemStruct->rcItem.left, nTop);
  116. pDC->SetBkColor(crefOldBk );
  117. pDC->SetTextColor(crefOldText );
  118. pDC->SelectObject(hBitmapOld);
  119. break;
  120. }
  121. }
  122. int CUserList::GetSelType(short sSel)
  123. {
  124. int sCount = 0;
  125. while (sCount < 5)
  126. {
  127. if (m_pBitmap[sCount] == (CTransBmp*)GetItemData(sSel)) return sCount;
  128. sCount ++;
  129. }
  130. return -1;
  131. }
  132. CString CUserList::GetGroupName(short sSel)
  133. {
  134. CString csText;
  135. GetText(sSel, csText);
  136. csText = csText.Left(csText.Find(L";"));
  137. return csText;
  138. }
  139. int CUserList::AddString(short nType, LPCTSTR lpItem)
  140. {
  141. int nPos = CListBox::AddString((const TCHAR*) lpItem);
  142. if (nPos == LB_ERR) return LB_ERR;
  143. SetItemData(nPos, (DWORD)m_pBitmap[nType]);
  144. return nPos;
  145. }
  146. int CUserList::AddString(LPCTSTR lpItem, DWORD dwBitmap)
  147. {
  148. int nPos = CListBox::AddString((const TCHAR*) lpItem);
  149. if (nPos == LB_ERR) return LB_ERR;
  150. SetItemData(nPos, dwBitmap);
  151. return nPos;
  152. }
  153. void CUserList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  154. {
  155. lpMeasureItemStruct->itemHeight = 18;
  156. }
  157. int CUserList::VKeyToItem(UINT nKey, UINT nIndex)
  158. {
  159. // TODO: Add your code to handle a particular virtual key
  160. // return -1 = default action
  161. // return -2 = no further action
  162. // return index = perform default action for keystroke on
  163. // item specified by index
  164. if (nKey == 46) DeleteString(GetCurSel());
  165. return -1;
  166. }
  167. int CUserList::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
  168. {
  169. TCHAR* pName1 = (TCHAR*)malloc(255 * sizeof(TCHAR));
  170. if (pName1 == NULL)
  171. {
  172. AfxMessageBox(IDS_GENERIC_NO_HEAP, MB_ICONEXCLAMATION);
  173. ExitProcess(1);
  174. }
  175. GetText(lpCompareItemStruct->itemID1, pName1);
  176. TCHAR* pName2 = (TCHAR*)malloc(255 * sizeof(TCHAR));
  177. if (pName2 == NULL)
  178. {
  179. AfxMessageBox(IDS_GENERIC_NO_HEAP, MB_ICONEXCLAMATION);
  180. ExitProcess(1);
  181. }
  182. GetText(lpCompareItemStruct->itemID2, pName2);
  183. int nRet = _tcsicmp(pName1, pName2);
  184. free(pName1);
  185. free(pName2);
  186. return nRet;
  187. }