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.

446 lines
11 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: ScInsBar.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. // ScInsBar.cpp : implementation file
  11. //
  12. #include "stdafx.h"
  13. #include "scdlg.h"
  14. #include "scinsdlg.h"
  15. #include "ScInsBar.h"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CScEdit special edit boxes (CardName, CardStatus)
  23. BEGIN_MESSAGE_MAP(CScEdit, CEdit)
  24. //{{AFX_MSG_MAP(CScEdit)
  25. ON_WM_CONTEXTMENU()
  26. //}}AFX_MSG_MAP
  27. END_MESSAGE_MAP()
  28. void CScEdit::OnContextMenu(CWnd* pWnd, CPoint pt)
  29. {
  30. ::WinHelp(m_hWnd, _T("SCardDlg.hlp"), HELP_CONTEXTMENU, (DWORD_PTR)(PVOID)g_aHelpIDs_IDD_SCARDDLG_BAR);
  31. }
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CScInsertBar dialog
  34. CScInsertBar::CScInsertBar(CWnd* pParent /*=NULL*/)
  35. : CDialog(CScInsertBar::IDD, pParent)
  36. {
  37. m_paReaderState = NULL;
  38. //{{AFX_DATA_INIT(CScInsertBar)
  39. // NOTE: the ClassWizard will add member initialization here
  40. //}}AFX_DATA_INIT
  41. }
  42. void CScInsertBar::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CDialog::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CScInsertBar)
  46. DDX_Control(pDX, IDC_NAME, m_ediName);
  47. DDX_Control(pDX, IDC_STATUS, m_ediStatus);
  48. DDX_Control(pDX, IDC_READERS, m_lstReaders);
  49. //}}AFX_DATA_MAP
  50. }
  51. void CScInsertBar::OnCancel()
  52. {
  53. CScInsertDlg* pParent = (CScInsertDlg*)GetParent();
  54. _ASSERTE(NULL != pParent);
  55. if (NULL != pParent)
  56. {
  57. pParent->PostMessage(IDCANCEL);
  58. }
  59. }
  60. BEGIN_MESSAGE_MAP(CScInsertBar, CDialog)
  61. //{{AFX_MSG_MAP(CScInsertBar)
  62. ON_WM_DESTROY()
  63. ON_NOTIFY(LVN_ITEMCHANGED, IDC_READERS, OnReaderItemChanged)
  64. ON_WM_HELPINFO()
  65. ON_WM_CONTEXTMENU()
  66. //}}AFX_MSG_MAP
  67. END_MESSAGE_MAP()
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CScInsertBar UI & smart card methods
  70. //
  71. // Hack for fixing Fusion problems caused by the CImageList being created
  72. // by mfc42u.dll instead of comctl32.dll. I'll force the create call to be
  73. // made in comctl32 and then pass the result to mfc42u.
  74. //
  75. // Without this change, the ImageList icons don't get displayed when launched
  76. // in the context of the MMC Certificate snap-in.
  77. //
  78. BOOL MyCreateImageList(CImageList &This, int cx, int cy, UINT nFlags, int nInitial, int nGrow)
  79. {
  80. return This.Attach(ImageList_Create(cx, cy, nFlags, nInitial, nGrow));
  81. }
  82. /*++
  83. InitializeReaderList:
  84. Initialize the list control w/ large images, and set up the
  85. CStringArray of image (reader/card status) descriptions...
  86. Arguments:
  87. None.
  88. Return Value:
  89. None.
  90. Author:
  91. Amanda Matlosz 07/14/1998
  92. --*/
  93. void CScInsertBar::InitializeReaderList(void)
  94. {
  95. HICON hicon;
  96. CImageList imageList;
  97. CString str;
  98. // Create the image list & give it to the list control
  99. MyCreateImageList(imageList, IMAGE_WIDTH, IMAGE_HEIGHT, TRUE, NUMBER_IMAGES, 0);
  100. // Build the image list
  101. for (int i = 0; i < NUMBER_IMAGES; i++ )
  102. {
  103. // Load icon and add it to image list
  104. hicon = NULL;
  105. hicon = ::LoadIcon ( AfxGetInstanceHandle(),
  106. MAKEINTRESOURCE(IMAGE_LIST_IDS[i]) );
  107. if (NULL==hicon) {
  108. break; // what can we do?
  109. }
  110. imageList.Add (hicon);
  111. }
  112. // Be sure that all the small icons were added.
  113. _ASSERTE(imageList.GetImageCount() == NUMBER_IMAGES);
  114. m_lstReaders.SetImageList(&imageList, (int) LVSIL_NORMAL);
  115. imageList.Detach();
  116. }
  117. /*++
  118. UpdateStatusList:
  119. This routine resets the list box display
  120. Arguments:
  121. None.
  122. Return Value:
  123. A LONG value indicating the status of the requested action. Please
  124. see the Smartcard header files for additional information.
  125. Author:
  126. Amanda Matlosz 06/15/1998
  127. Notes:
  128. Strings need to be converted from type stored in the smartcard
  129. thread help classes to this dialog's build type (i.e. UNICODE/ANSI)!!!!
  130. --*/
  131. void CScInsertBar::UpdateStatusList(CSCardReaderStateArray* paReaderState)
  132. {
  133. CString strCardStatus, strCardName;
  134. CSCardReaderState* pReader = NULL;
  135. CSCardReaderState* pSelectedRdr = NULL;
  136. LV_ITEM lv_item;
  137. //
  138. // Update the reader information
  139. //
  140. m_paReaderState = paReaderState;
  141. // reset previous knowledge re: reader/card status
  142. m_ediName.SetWindowText(_T(""));
  143. m_ediStatus.SetWindowText(_T(""));
  144. m_lstReaders.DeleteAllItems();
  145. if (NULL != m_paReaderState)
  146. {
  147. // Insert (new) items
  148. lv_item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
  149. lv_item.cchTextMax = MAX_ITEMLEN;
  150. int nNumReaders = (int)m_paReaderState->GetSize();
  151. for(int nIndex = 0; nIndex < nNumReaders; nIndex++)
  152. {
  153. // Setup struct for system reader list
  154. pReader = m_paReaderState->GetAt(nIndex);
  155. _ASSERTE(NULL != pReader);
  156. lv_item.iItem = nIndex;
  157. lv_item.stateMask = 0;
  158. lv_item.state = 0;
  159. lv_item.iSubItem = 0;
  160. lv_item.iImage = (int)READEREMPTY;
  161. lv_item.pszText = NULL;
  162. // set lparam to the reader ptr so we can fetch the readerinfo later
  163. lv_item.lParam = (LPARAM)pReader;
  164. //
  165. // Get the card status: image, and select OK card
  166. //
  167. if (NULL != pReader)
  168. {
  169. lv_item.pszText = (LPTSTR)(LPCTSTR)(pReader->strReader);
  170. DWORD dwState = pReader->dwState;
  171. if (dwState == SC_STATUS_NO_CARD)
  172. {
  173. lv_item.iImage = (int)READEREMPTY;
  174. }
  175. else if (dwState == SC_STATUS_ERROR)
  176. {
  177. lv_item.iImage = (int)READERERROR;
  178. }
  179. else
  180. {
  181. if (pReader->fOK)
  182. {
  183. lv_item.iImage = (int)READERLOADED;
  184. }
  185. else
  186. {
  187. lv_item.iImage = (int)WRONGCARD;
  188. }
  189. }
  190. // Select if this is a search card
  191. if (pReader->fOK && (NULL==pSelectedRdr))
  192. {
  193. lv_item.state = LVIS_SELECTED | LVIS_FOCUSED;
  194. // Set that a selection has occurred
  195. pSelectedRdr = pReader;
  196. }
  197. }
  198. // Add Item
  199. m_lstReaders.InsertItem(&lv_item);
  200. }
  201. // indicate that the reader selection has changed
  202. if (NULL != pSelectedRdr)
  203. {
  204. OnReaderSelChange(pSelectedRdr);
  205. }
  206. else
  207. {
  208. // select the first item in the list
  209. m_lstReaders.SetItemState(0, LVIS_SELECTED | LVIS_FOCUSED, 0);
  210. OnReaderSelChange(m_paReaderState->GetAt(0));
  211. }
  212. m_lstReaders.SetFocus(); // TODO: ?? Remove this? ??
  213. }
  214. }
  215. /////////////////////////////////////////////////////////////////////////////
  216. // CScInsertBar message handlers
  217. /*++
  218. void ShowHelp:
  219. Helper function for OnHelpInfo and OnContextMenu.
  220. BOOL OnHelpInfo:
  221. Called by the MFC framework when the user hits F1.
  222. void OnContextMenu
  223. Called by the MFC framework when the user right-clicks.
  224. Author:
  225. Amanda Matlosz 03/04/1999
  226. Note:
  227. These three functions work together to provide context-sensitive
  228. help for the insertdlg.
  229. --*/
  230. void CScInsertBar::ShowHelp(HWND hWnd, UINT nCommand)
  231. {
  232. ::WinHelp(hWnd, _T("SCardDlg.hlp"), nCommand, (DWORD_PTR)(PVOID)g_aHelpIDs_IDD_SCARDDLG_BAR);
  233. }
  234. afx_msg BOOL CScInsertBar::OnHelpInfo(LPHELPINFO lpHelpInfo)
  235. {
  236. _ASSERTE(NULL != lpHelpInfo);
  237. ShowHelp((HWND)lpHelpInfo->hItemHandle, HELP_WM_HELP);
  238. return TRUE;
  239. }
  240. afx_msg void CScInsertBar::OnContextMenu(CWnd* pWnd, CPoint pt)
  241. {
  242. _ASSERTE(NULL != pWnd);
  243. ShowHelp(pWnd->m_hWnd, HELP_CONTEXTMENU);
  244. }
  245. void CScInsertBar::OnDestroy()
  246. {
  247. // clean up image list
  248. m_SCardImages.DeleteImageList();
  249. CDialog::OnDestroy();
  250. }
  251. BOOL CScInsertBar::OnInitDialog()
  252. {
  253. CDialog::OnInitDialog();
  254. //
  255. // prepare list control
  256. //
  257. InitializeReaderList();
  258. //
  259. // TODO: try SubclassWindow() trick. What's up with MFC?
  260. //
  261. CWnd* pEdit = NULL;
  262. pEdit = GetDlgItem(IDC_NAME);
  263. if (NULL != pEdit) m_ediName.SubclassWindow(pEdit->m_hWnd);
  264. pEdit = NULL;
  265. pEdit = GetDlgItem(IDC_STATUS);
  266. if (NULL != pEdit) m_ediStatus.SubclassWindow(pEdit->m_hWnd);
  267. return TRUE; // return TRUE unless you set the focus to a control
  268. // EXCEPTION: OCX Property Pages should return FALSE
  269. }
  270. /*++
  271. OnReaderItemChanged:
  272. Routine processes a selection change in the list control --
  273. if a card name is selected, it is displayed in a separate control
  274. Arguments:
  275. pNMHDR - pointer to notification structure
  276. pResult - pointer to LRESULT
  277. Return Value:
  278. Returns TRUE on success; FALSE otherwise.
  279. Author:
  280. Amanda Matlosz 09/26/1998
  281. Revision History:
  282. --*/
  283. void CScInsertBar::OnReaderItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
  284. {
  285. int nItem = m_lstReaders.GetNextItem(-1, LVNI_SELECTED);
  286. if (nItem != -1)
  287. {
  288. CSCardReaderState* pRdrSt = (CSCardReaderState*)m_lstReaders.GetItemData(nItem);
  289. OnReaderSelChange(pRdrSt);
  290. }
  291. *pResult = 0;
  292. }
  293. void CScInsertBar::OnReaderSelChange(CSCardReaderState* pSelectedRdr)
  294. {
  295. _ASSERTE(pSelectedRdr);
  296. if (NULL != pSelectedRdr)
  297. {
  298. //
  299. // Change UI to show selection details
  300. //
  301. CString strStatus, strName;
  302. DWORD dwState = pSelectedRdr->dwState;
  303. strStatus.LoadString(IDS_SC_STATUS_NO_CARD + dwState - SC_STATUS_NO_CARD);
  304. if (dwState != SC_STATUS_NO_CARD)
  305. {
  306. strName = pSelectedRdr->strCard;
  307. strName.TrimLeft();
  308. if (strName.IsEmpty() || dwState == SC_STATUS_UNKNOWN)
  309. {
  310. strName.LoadString(IDS_SC_NAME_UNKNOWN);
  311. }
  312. if (!pSelectedRdr->fOK && (dwState >= SC_SATATUS_AVAILABLE && dwState <= SC_STATUS_EXCLUSIVE))
  313. {
  314. CString strAdd;
  315. strAdd.LoadString(IDS_SC_CANT_USE);
  316. strStatus += " ";
  317. strStatus += strAdd;
  318. }
  319. }
  320. m_ediName.SetWindowText(strName);
  321. m_ediStatus.SetWindowText(strStatus);
  322. }
  323. //
  324. // Inform parent of change in selection, even if that sel is "NULL"
  325. //
  326. CScInsertDlg* pParent = (CScInsertDlg*)GetParent();
  327. _ASSERTE(NULL != pParent);
  328. if (NULL != pParent)
  329. {
  330. pParent->SetSelection(pSelectedRdr);
  331. }
  332. }
  333.