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.

690 lines
21 KiB

  1. /****************************************************************************
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name: cplcallingcardtab.cpp
  4. Author: toddb - 10/06/98
  5. ****************************************************************************/
  6. //
  7. // Functions used only by the Calling Card tab of the New Location Property Sheet.
  8. // Shared functions are in the Location.cpp file.
  9. //
  10. #include "cplPreComp.h"
  11. #include "cplLocationPS.h"
  12. INT_PTR CALLBACK CLocationPropSheet::CallingCard_DialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  13. {
  14. CLocationPropSheet* pthis = (CLocationPropSheet*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  15. switch(uMsg)
  16. {
  17. case WM_INITDIALOG:
  18. pthis = (CLocationPropSheet*)(((PROPSHEETPAGE*)lParam)->lParam);
  19. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR) pthis);
  20. return pthis->CallingCard_OnInitDialog(hwndDlg);
  21. case WM_COMMAND:
  22. pthis->CallingCard_OnCommand(hwndDlg, LOWORD(wParam), HIWORD(wParam), (HWND)lParam );
  23. return 1;
  24. case WM_NOTIFY:
  25. return pthis->CallingCard_OnNotify(hwndDlg, (LPNMHDR)lParam);
  26. case WM_HELP:
  27. // Process clicks on controls after Context Help mode selected
  28. TapiCplWinHelp ((HWND)((LPHELPINFO)lParam)->hItemHandle, gszHelpFile, HELP_WM_HELP, (DWORD_PTR)(LPTSTR) a104HelpIDs);
  29. break;
  30. case WM_CONTEXTMENU:
  31. // Process right-clicks on controls
  32. TapiCplWinHelp ((HWND) wParam, gszHelpFile, HELP_CONTEXTMENU, (DWORD_PTR)(LPVOID) a104HelpIDs);
  33. break;
  34. }
  35. return 0;
  36. }
  37. BOOL CLocationPropSheet::CallingCard_OnInitDialog(HWND hDlg)
  38. {
  39. RECT rc;
  40. HWND hwnd = GetDlgItem(hDlg, IDC_LIST);
  41. GetClientRect(hwnd, &rc);
  42. LVCOLUMN lvc;
  43. lvc.mask = LVCF_SUBITEM | LVCF_WIDTH;
  44. lvc.iSubItem = 0;
  45. lvc.cx = rc.right - GetSystemMetrics(SM_CXVSCROLL);
  46. ListView_InsertColumn( hwnd, 0, &lvc );
  47. ListView_SetExtendedListViewStyleEx(hwnd,
  48. LVS_EX_LABELTIP | LVS_EX_FULLROWSELECT,
  49. LVS_EX_LABELTIP | LVS_EX_FULLROWSELECT);
  50. m_dwDefaultCard = m_pLoc->GetPreferredCardID();
  51. if ( 0 == m_dwDefaultCard )
  52. {
  53. // Card0 is the "None (Direct Dial)" card which we want to go away
  54. m_pLoc->UseCallingCard(FALSE);
  55. }
  56. PopulateCardList( hwnd );
  57. // The PIN is not displayed when it's not safe (at logon time, for ex.)
  58. m_bShowPIN = TapiIsSafeToDisplaySensitiveData();
  59. SetDataForSelectedCard(hDlg);
  60. hwnd = GetDlgItem(hDlg,IDC_CARDNUMBER);
  61. SendMessage(hwnd,EM_SETLIMITTEXT,CPL_SETTEXTLIMIT,0);
  62. LimitInput(hwnd, LIF_ALLOWNUMBER|LIF_ALLOWSPACE);
  63. hwnd = GetDlgItem(hDlg,IDC_PIN);
  64. SendMessage(hwnd,EM_SETLIMITTEXT,CPL_SETTEXTLIMIT,0);
  65. LimitInput(hwnd, LIF_ALLOWNUMBER|LIF_ALLOWSPACE);
  66. return 0;
  67. }
  68. int CALLBACK CallingCard_ListSort(LPARAM lItem1, LPARAM lItem2, LPARAM )
  69. {
  70. if ( !lItem1 )
  71. {
  72. return -1;
  73. }
  74. if ( !lItem2 )
  75. {
  76. return 1;
  77. }
  78. CCallingCard * pCard1 = (CCallingCard *)lItem1;
  79. CCallingCard * pCard2 = (CCallingCard *)lItem2;
  80. return StrCmpIW(pCard1->GetCardName(),pCard2->GetCardName());
  81. }
  82. void CLocationPropSheet::PopulateCardList( HWND hwndList )
  83. {
  84. CCallingCard * pCard;
  85. HIMAGELIST himl = ImageList_Create(16, 16, ILC_COLOR|ILC_MASK, 2, 2);
  86. HBITMAP hBmp = CreateMappedBitmap(GetUIInstance(), IDB_BUTTONS, 0, NULL, 0);
  87. if (NULL != hBmp)
  88. {
  89. ImageList_AddMasked( himl, hBmp, CLR_DEFAULT);
  90. DeleteObject( hBmp );
  91. }
  92. ListView_SetImageList(hwndList, himl, LVSIL_SMALL);
  93. // Add our special "none" item
  94. AddCardToList(hwndList,NULL,FALSE);
  95. m_Cards.Initialize();
  96. m_Cards.Reset(TRUE); // TRUE means show "hidden" cards, FALSE means hide them
  97. while ( S_OK == m_Cards.Next(1,&pCard,NULL) )
  98. {
  99. if ( !pCard->IsMarkedHidden() )
  100. {
  101. // Card0 is the "None (Direct Dial)" card which we don't want to show
  102. if ( 0 != pCard->GetCardID() )
  103. {
  104. AddCardToList(hwndList,pCard,FALSE);
  105. }
  106. }
  107. }
  108. ListView_SortItems(hwndList, CallingCard_ListSort, 0);
  109. EnsureVisible(hwndList, m_pCard);
  110. }
  111. void CLocationPropSheet::AddCardToList(HWND hwndList, CCallingCard * pCard, BOOL bSelect)
  112. {
  113. TCHAR szText[MAX_INPUT];
  114. // basically, bSelect is FALSE when we are first populating the list and TRUE when we
  115. // add items later. When the value is FALSE what we really mean is "Select the item
  116. // only if it is the currently selected item based on the location settings".
  117. if (pCard)
  118. {
  119. SHUnicodeToTChar(pCard->GetCardName(), szText, ARRAYSIZE(szText));
  120. bSelect = bSelect || ((m_dwDefaultCard != 0) && (m_dwDefaultCard==pCard->GetCardID()));
  121. }
  122. else
  123. {
  124. LoadString(GetUIInstance(), IDS_NONE, szText, ARRAYSIZE(szText));
  125. bSelect = bSelect || !(m_dwDefaultCard != 0);
  126. }
  127. LVITEM lvi;
  128. lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
  129. lvi.iItem = 0;
  130. lvi.iSubItem = 0;
  131. lvi.pszText = szText;
  132. lvi.iImage = 0;
  133. lvi.lParam = (LPARAM)pCard;
  134. if ( bSelect )
  135. {
  136. lvi.mask |= LVIF_STATE;
  137. lvi.state = lvi.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  138. lvi.iImage = 1;
  139. SetCheck(hwndList, m_pCard, FALSE);
  140. m_pCard = pCard;
  141. }
  142. ListView_InsertItem(hwndList, &lvi);
  143. }
  144. void CLocationPropSheet::SetCheck(HWND hwndList, CCallingCard * pCard, int iImage)
  145. {
  146. LVFINDINFO lvfi;
  147. lvfi.flags = LVFI_PARAM;
  148. lvfi.lParam = (LPARAM)pCard;
  149. int iItem = ListView_FindItem(hwndList,-1,&lvfi);
  150. if (-1 != iItem)
  151. {
  152. LVITEM lvi;
  153. lvi.mask = LVIF_IMAGE;
  154. lvi.iItem = iItem;
  155. lvi.iSubItem = 0;
  156. lvi.iImage = iImage;
  157. ListView_SetItem( hwndList, &lvi );
  158. ListView_Update( hwndList, iItem ); // need the font to be drawn non-bold
  159. }
  160. }
  161. void CLocationPropSheet::EnsureVisible(HWND hwndList, CCallingCard * pCard)
  162. {
  163. LVFINDINFO lvfi;
  164. lvfi.flags = LVFI_PARAM;
  165. lvfi.lParam = (LPARAM)pCard;
  166. int iItem = ListView_FindItem(hwndList,-1,&lvfi);
  167. if (-1 != iItem)
  168. {
  169. ListView_EnsureVisible( hwndList, iItem, FALSE );
  170. }
  171. }
  172. void CLocationPropSheet::UpdateCardInList(HWND hwndList, CCallingCard * pCard)
  173. {
  174. LVFINDINFO lvfi;
  175. lvfi.flags = LVFI_PARAM;
  176. lvfi.lParam = (LPARAM)pCard;
  177. int iItem = ListView_FindItem(hwndList,-1,&lvfi);
  178. if (-1 != iItem)
  179. {
  180. TCHAR szText[MAX_INPUT];
  181. SHUnicodeToTChar( pCard->GetCardName(), szText, ARRAYSIZE(szText) );
  182. LVITEM lvi;
  183. lvi.mask = LVIF_TEXT | LVIF_PARAM;
  184. lvi.iItem = iItem;
  185. lvi.iSubItem = 0;
  186. lvi.pszText = szText;
  187. lvi.lParam = (LONG_PTR)pCard;
  188. ListView_SetItem( hwndList, &lvi );
  189. }
  190. }
  191. void CLocationPropSheet::SetDataForSelectedCard(HWND hDlg)
  192. {
  193. // if a card is selected, then set the text for:
  194. // PIN Number
  195. // Card Number
  196. // Long Distance Access Number
  197. // International Access Number
  198. if ( m_pCard )
  199. {
  200. TCHAR szText[MAX_INPUT];
  201. if(m_bShowPIN)
  202. {
  203. SHUnicodeToTChar(m_pCard->GetPIN(), szText, ARRAYSIZE(szText));
  204. SetWindowText( GetDlgItem(hDlg, IDC_PIN), szText );
  205. }
  206. else
  207. {
  208. SetWindowText( GetDlgItem(hDlg, IDC_PIN), TEXT("") );
  209. }
  210. SHUnicodeToTChar(m_pCard->GetAccountNumber(), szText, ARRAYSIZE(szText));
  211. SetWindowText( GetDlgItem(hDlg, IDC_CARDNUMBER), szText );
  212. SHUnicodeToTChar(m_pCard->GetLongDistanceAccessNumber(), szText, ARRAYSIZE(szText));
  213. SetWindowText( GetDlgItem(hDlg, IDC_LONGDISTANCE), szText );
  214. SHUnicodeToTChar(m_pCard->GetInternationalAccessNumber(), szText, ARRAYSIZE(szText));
  215. SetWindowText( GetDlgItem(hDlg, IDC_INTERNATIONAL), szText );
  216. SHUnicodeToTChar(m_pCard->GetLocalAccessNumber(), szText, ARRAYSIZE(szText));
  217. SetWindowText( GetDlgItem(hDlg, IDC_LOCAL), szText );
  218. }
  219. else
  220. {
  221. SetWindowText( GetDlgItem(hDlg, IDC_PIN), TEXT("") );
  222. SetWindowText( GetDlgItem(hDlg, IDC_CARDNUMBER), TEXT("") );
  223. SetWindowText( GetDlgItem(hDlg, IDC_LONGDISTANCE), TEXT("") );
  224. SetWindowText( GetDlgItem(hDlg, IDC_INTERNATIONAL), TEXT("") );
  225. SetWindowText( GetDlgItem(hDlg, IDC_LOCAL), TEXT("") );
  226. }
  227. // The button state depends on whether a card is selected
  228. BOOL bEnable = 0!=m_pCard;
  229. EnableWindow( GetDlgItem(hDlg, IDC_EDIT), bEnable );
  230. HWND hwnd = GetDlgItem(hDlg, IDC_DELETE);
  231. if ( !bEnable && GetFocus() == hwnd )
  232. {
  233. HWND hwndDef = GetDlgItem(hDlg, IDC_NEW);
  234. SendMessage(hwnd, BM_SETSTYLE, BS_PUSHBUTTON, MAKELPARAM(TRUE,0));
  235. SendMessage(hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, MAKELPARAM(TRUE,0));
  236. SetFocus(hwndDef);
  237. }
  238. EnableWindow( hwnd, bEnable );
  239. EnableWindow( GetDlgItem(hDlg, IDC_SETDEFAULT), bEnable );
  240. EnableWindow( GetDlgItem(hDlg, IDC_PIN), bEnable );
  241. EnableWindow( GetDlgItem(hDlg, IDC_CARDNUMBER), bEnable );
  242. }
  243. BOOL CLocationPropSheet::CallingCard_OnCommand(HWND hwndParent, int wID, int wNotifyCode, HWND hwndCrl)
  244. {
  245. switch ( wID )
  246. {
  247. case IDC_NEW:
  248. case IDC_EDIT:
  249. LaunchCallingCardPropSheet(IDC_NEW == wID, hwndParent);
  250. break;
  251. case IDC_DELETE:
  252. DeleteSelectedCard(GetDlgItem(hwndParent,IDC_LIST));
  253. break;
  254. case IDC_PIN:
  255. case IDC_CARDNUMBER:
  256. if ( EN_CHANGE == wNotifyCode )
  257. {
  258. SendMessage(GetParent(hwndParent),PSM_CHANGED,(WPARAM)hwndParent,0);
  259. }
  260. break;
  261. default:
  262. return 0;
  263. }
  264. return 1;
  265. }
  266. void CLocationPropSheet::LaunchCallingCardPropSheet(BOOL bNew, HWND hwndParent)
  267. {
  268. CCallingCard * pCard;
  269. if ( bNew )
  270. {
  271. TCHAR szCardName[MAX_INPUT];
  272. WCHAR wszCardName[MAX_INPUT];
  273. pCard = new CCallingCard;
  274. if (NULL == pCard)
  275. {
  276. // Nothing much to do.
  277. return;
  278. }
  279. LoadString(GetUIInstance(), IDS_NEWCALLINGCARD, szCardName, ARRAYSIZE(szCardName));
  280. SHTCharToUnicode(szCardName, wszCardName, ARRAYSIZE(wszCardName));
  281. pCard->Initialize(
  282. 0,
  283. wszCardName,
  284. 0,
  285. L"",
  286. L"",
  287. L"",
  288. L"",
  289. L"",
  290. L"",
  291. L"",
  292. L"" );
  293. }
  294. else
  295. {
  296. pCard = m_pCard;
  297. if ( !pCard )
  298. {
  299. // must have clicked on the None card, do nothing. We can only get
  300. // here when the user double clicks on an item.
  301. MessageBeep(0);
  302. return;
  303. }
  304. }
  305. CCallingCardPropSheet ccps( bNew, m_bShowPIN, pCard, &m_Cards );
  306. int iRes = ccps.DoPropSheet(hwndParent);
  307. if ( PSN_APPLY == iRes )
  308. {
  309. HWND hwndList = GetDlgItem(hwndParent,IDC_LIST);
  310. if ( bNew )
  311. {
  312. pCard->SetCardID(m_Cards.AllocNewCardID());
  313. m_Cards.AddCard(pCard);
  314. AddCardToList(hwndList, pCard, TRUE);
  315. }
  316. else
  317. {
  318. UpdateCardInList(hwndList, pCard);
  319. }
  320. ListView_SortItems(hwndList, CallingCard_ListSort, 0);
  321. EnsureVisible(hwndList, pCard);
  322. // It's safe to display the PIN number after an Apply in the detail dialog
  323. m_bShowPIN = TRUE;
  324. SetDataForSelectedCard(hwndParent);
  325. SendMessage(GetParent(hwndParent),PSM_CHANGED,(WPARAM)hwndParent,0);
  326. }
  327. else if (bNew)
  328. {
  329. delete pCard;
  330. }
  331. }
  332. BOOL CLocationPropSheet::CallingCard_OnNotify(HWND hwndDlg, LPNMHDR pnmhdr)
  333. {
  334. // Let the generic handler have a crack at it first
  335. OnNotify(hwndDlg, pnmhdr);
  336. switch (pnmhdr->idFrom)
  337. {
  338. case IDC_LIST:
  339. #define pnmlv ((LPNMLISTVIEW)pnmhdr)
  340. switch (pnmhdr->code)
  341. {
  342. case LVN_ITEMCHANGED:
  343. if ( (pnmlv->uChanged & LVIF_STATE) && (pnmlv->uNewState & LVIS_SELECTED) )
  344. {
  345. LVITEM lvi;
  346. lvi.iItem = pnmlv->iItem;
  347. lvi.iSubItem = pnmlv->iSubItem;
  348. lvi.mask = LVIF_PARAM;
  349. ListView_GetItem( pnmhdr->hwndFrom, &lvi );
  350. CCallingCard * pCard = (CCallingCard *)lvi.lParam;
  351. // update the location to reflect the selected card
  352. if ( 0!=pCard )
  353. {
  354. m_dwDefaultCard = pCard->GetCardID();
  355. }
  356. else
  357. {
  358. m_dwDefaultCard = 0;
  359. }
  360. // clear the previous check using the old m_pCard value
  361. SetCheck(pnmhdr->hwndFrom, m_pCard, FALSE);
  362. // Update m_pCard to the currently selected item
  363. m_pCard = pCard;
  364. // set the Edit and Delete button states and update the card info
  365. m_bShowPIN = TapiIsSafeToDisplaySensitiveData();
  366. SetDataForSelectedCard(hwndDlg);
  367. // set the newly selected card to checked
  368. SetCheck(pnmhdr->hwndFrom, m_pCard, TRUE);
  369. }
  370. break;
  371. case NM_DBLCLK:
  372. // Assert( pCard == m_pCard );
  373. if ( -1 != pnmlv->iItem )
  374. {
  375. // Do edit case
  376. LaunchCallingCardPropSheet(FALSE,hwndDlg);
  377. }
  378. else
  379. {
  380. // Do new case
  381. LaunchCallingCardPropSheet(TRUE,hwndDlg);
  382. }
  383. break;
  384. case NM_CUSTOMDRAW:
  385. #define lplvcd ((LPNMLVCUSTOMDRAW)pnmhdr)
  386. if(lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
  387. {
  388. // Request prepaint notifications for each item.
  389. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,CDRF_NOTIFYITEMDRAW);
  390. return CDRF_NOTIFYITEMDRAW;
  391. }
  392. if(lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
  393. {
  394. LVITEM lvi;
  395. lvi.iItem = (int)lplvcd->nmcd.dwItemSpec;
  396. lvi.iSubItem = 0;
  397. lvi.mask = LVIF_PARAM;
  398. ListView_GetItem( pnmhdr->hwndFrom, &lvi );
  399. CCallingCard * pCard = (CCallingCard *)lvi.lParam;
  400. if( (!pCard && 0 == m_dwDefaultCard) ||
  401. (pCard && pCard->GetCardID() == m_dwDefaultCard) )
  402. {
  403. extern HFONT g_hfontBold;
  404. if (g_hfontBold)
  405. {
  406. SelectObject(lplvcd->nmcd.hdc, g_hfontBold);
  407. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,CDRF_NEWFONT);
  408. return CDRF_NEWFONT;
  409. }
  410. }
  411. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,CDRF_DODEFAULT);
  412. return CDRF_DODEFAULT;
  413. }
  414. return 0;
  415. #undef lplvcd
  416. default:
  417. break;
  418. }
  419. break;
  420. #undef pnmlv
  421. default:
  422. switch (pnmhdr->code)
  423. {
  424. case PSN_APPLY:
  425. return CallingCard_OnApply(hwndDlg);
  426. default:
  427. break;
  428. }
  429. return 0;
  430. }
  431. return 1;
  432. }
  433. BOOL CLocationPropSheet::CallingCard_OnApply(HWND hwndDlg)
  434. {
  435. // if a calling card should be used make sure one is selected
  436. if ( m_dwDefaultCard != 0 )
  437. {
  438. CCallingCard * pCard = m_Cards.GetCallingCard(m_dwDefaultCard);
  439. if ( !pCard )
  440. {
  441. HWND hwndList = GetDlgItem(hwndDlg,IDC_LIST);
  442. // error, no card is set as the default
  443. PropSheet_SetCurSelByID(GetParent(hwndDlg),IDD_LOC_CALLINGCARD);
  444. ShowErrorMessage(hwndList, IDS_NOCARDSELECTED);
  445. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,PSNRET_INVALID_NOCHANGEPAGE);
  446. return TRUE;
  447. }
  448. // Store the original values before we change them:
  449. WCHAR wszOldCardNum[128];
  450. WCHAR wszOldPIN[128];
  451. StrCpyNW( wszOldCardNum, pCard->GetAccountNumber(), ARRAYSIZE(wszOldCardNum));
  452. StrCpyNW( wszOldPIN, pCard->GetPIN(), ARRAYSIZE(wszOldPIN));
  453. // get the current values:
  454. TCHAR szText[MAX_INPUT];
  455. WCHAR wszBuf[MAX_INPUT];
  456. GetWindowText(GetDlgItem(hwndDlg,IDC_CARDNUMBER), szText, ARRAYSIZE(szText));
  457. LOG((TL_INFO, "CallingCard_OnApply: Setting card number to %s", szText));
  458. SHTCharToUnicode(szText, wszBuf, ARRAYSIZE(wszBuf));
  459. pCard->SetAccountNumber(wszBuf);
  460. GetWindowText(GetDlgItem(hwndDlg,IDC_PIN), szText, ARRAYSIZE(szText));
  461. LOG((TL_INFO, "CallingCard_OnApply: Setting pin number to %s", szText));
  462. SHTCharToUnicode(szText, wszBuf, ARRAYSIZE(wszBuf));
  463. pCard->SetPIN(wszBuf);
  464. // check for validity:
  465. DWORD dwResult = pCard->Validate();
  466. if ( dwResult )
  467. {
  468. HWND hwnd;
  469. int iStrID;
  470. // something isn't valid, revert to old card Num and PIN in case
  471. // the user later decided to cancel
  472. pCard->SetAccountNumber(wszOldCardNum);
  473. pCard->SetPIN(wszOldPIN);
  474. if ( dwResult & CCVF_NOCARDNUMBER)
  475. {
  476. hwnd = GetDlgItem(hwndDlg, IDC_CARDNUMBER);
  477. iStrID = IDS_MUSTENTERCARDNUMBER;
  478. }
  479. else if ( dwResult & CCVF_NOPINNUMBER )
  480. {
  481. hwnd = GetDlgItem(hwndDlg, IDC_PIN);
  482. iStrID = IDS_MUSTENTERPINNUMBER;
  483. }
  484. else
  485. {
  486. hwnd = GetDlgItem(hwndDlg, IDC_LIST);
  487. iStrID = IDS_INVALIDCARD;
  488. }
  489. PropSheet_SetCurSelByID(GetParent(hwndDlg),IDD_LOC_CALLINGCARD);
  490. ShowErrorMessage(hwnd, iStrID);
  491. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,PSNRET_INVALID_NOCHANGEPAGE);
  492. return TRUE;
  493. }
  494. }
  495. m_pLoc->SetPreferredCardID(m_dwDefaultCard);
  496. m_pLoc->UseCallingCard(m_dwDefaultCard != 0);
  497. m_Cards.SaveToRegistry();
  498. m_bShowPIN = TRUE;
  499. m_bWasApplied = TRUE;
  500. return PSNRET_NOERROR;
  501. }
  502. int DeleteItemAndSelectFirst( HWND hwndParent, int iList, int iItem, int iDel, int iAdd )
  503. {
  504. HWND hwnd = GetDlgItem(hwndParent, iList);
  505. ListView_DeleteItem(hwnd, iItem);
  506. // Try to select the first item, if possible
  507. iItem = 0;
  508. LVITEM lvi;
  509. lvi.mask = LVIF_PARAM;
  510. lvi.iItem = iItem;
  511. lvi.iSubItem = 0;
  512. if ( ListView_GetItem(hwnd, &lvi) )
  513. {
  514. ListView_SetItemState(hwnd, iItem, LVIS_FOCUSED|LVIS_SELECTED, LVIS_FOCUSED|LVIS_SELECTED);
  515. ListView_EnsureVisible(hwnd, iItem, FALSE);
  516. }
  517. else
  518. {
  519. iItem = -1;
  520. }
  521. hwnd = GetDlgItem(hwndParent,iDel);
  522. if ( -1 == iItem )
  523. {
  524. if ( GetFocus() == hwnd )
  525. {
  526. HWND hwndDef = GetDlgItem(hwndParent,iAdd);
  527. SendMessage(hwnd, BM_SETSTYLE, BS_PUSHBUTTON, MAKELPARAM(TRUE,0));
  528. SendMessage(hwndDef, BM_SETSTYLE, BS_DEFPUSHBUTTON, MAKELPARAM(TRUE,0));
  529. SetFocus(hwndDef);
  530. }
  531. }
  532. EnableWindow(hwnd, -1!=iItem);
  533. return iItem;
  534. }
  535. void CLocationPropSheet::DeleteSelectedCard(HWND hwndList)
  536. {
  537. // First we confirm the delete with the user
  538. TCHAR szText[1024];
  539. TCHAR szTitle[128];
  540. int result;
  541. HWND hwndParent = GetParent(hwndList);
  542. LoadString(GetUIInstance(), IDS_DELETECARDTEXT, szText, ARRAYSIZE(szText));
  543. LoadString(GetUIInstance(), IDS_CONFIRMDELETE, szTitle, ARRAYSIZE(szTitle));
  544. result = SHMessageBoxCheck( hwndParent, szText, szTitle, MB_YESNO, IDYES, TEXT("TAPIDeleteCallingCard") );
  545. if ( IDYES == result )
  546. {
  547. // remove the item corresponding to m_pCard from the list
  548. LVFINDINFO lvfi;
  549. lvfi.flags = LVFI_PARAM;
  550. lvfi.lParam = (LPARAM)m_pCard;
  551. int iItem = ListView_FindItem(hwndList, -1, &lvfi);
  552. if ( -1 != iItem )
  553. {
  554. HWND hwndParent = GetParent(hwndList);
  555. m_Cards.RemoveCard(m_pCard);
  556. iItem = DeleteItemAndSelectFirst( hwndParent, IDC_LIST, iItem, IDC_DELETE, IDC_ADD );
  557. if ( -1 != iItem )
  558. {
  559. LVITEM lvi;
  560. lvi.iItem = iItem;
  561. lvi.iSubItem = 0;
  562. lvi.mask = LVIF_PARAM;
  563. ListView_GetItem( hwndList, &lvi );
  564. // Store the currently selected item
  565. m_pCard = (CCallingCard*)lvi.lParam;
  566. }
  567. else
  568. {
  569. m_pCard = NULL;
  570. }
  571. m_bShowPIN = TapiIsSafeToDisplaySensitiveData();
  572. SetDataForSelectedCard(hwndParent);
  573. SendMessage(GetParent(hwndParent), PSM_CHANGED, (WPARAM)hwndParent, 0);
  574. }
  575. else
  576. {
  577. // It's really bad if this ever happens (which it shouldn't). This means our
  578. // data is in an unknown state and we might do anything (even destroy data).
  579. LOG((TL_ERROR, "DeleteSelectedCard: Card Not Found!"));
  580. }
  581. }
  582. }