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.

221 lines
4.2 KiB

  1. // CountryComboBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CertWiz.h"
  5. #include "CountryComboBox.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. BEGIN_MESSAGE_MAP(CComboEdit, CEdit)
  12. ON_WM_CHAR()
  13. END_MESSAGE_MAP()
  14. void CComboEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  15. {
  16. if (m_pParent->OnEditChar(nChar))
  17. CEdit::OnChar(nChar, nRepCnt, nFlags);
  18. }
  19. BOOL CComboEdit::SubclassDlgItem(UINT nID, CCountryComboBox * pParent)
  20. {
  21. ASSERT(pParent != NULL);
  22. m_pParent = pParent;
  23. return CEdit::SubclassDlgItem(nID, pParent);
  24. }
  25. BOOL CCountryComboBox::OnEditChar(UINT nChar)
  26. {
  27. int index;
  28. int len = m_strInput.GetLength();
  29. if (nChar == VK_ESCAPE)
  30. {
  31. if (len == 0)
  32. {
  33. MessageBeep(MB_ICONQUESTION);
  34. return FALSE;
  35. }
  36. m_strInput.Empty();
  37. len = 0;
  38. }
  39. else if (nChar == VK_BACK)
  40. {
  41. if (len == 0)
  42. {
  43. MessageBeep(MB_ICONQUESTION);
  44. return FALSE;
  45. }
  46. m_strInput.ReleaseBuffer(--len);
  47. }
  48. else if (_istalpha((TCHAR) nChar) || VK_SPACE == nChar)
  49. {
  50. m_strInput += (TCHAR)nChar;
  51. len++;
  52. }
  53. else
  54. {
  55. MessageBeep(MB_ICONQUESTION);
  56. return FALSE;
  57. }
  58. if (len > 0 && len <= 2)
  59. {
  60. if (CB_ERR != (index = FindString(-1, m_strInput)))
  61. {
  62. m_Index = index;
  63. SetCurSel(m_Index);
  64. SetEditSel(0, m_strInput.GetLength());
  65. }
  66. else
  67. {
  68. // try to find it in country names list
  69. index = -1;
  70. POSITION pos = m_map_name_code.GetStartPosition();
  71. int i = 0;
  72. while (pos != NULL)
  73. {
  74. CString name, code;
  75. m_map_name_code.GetNextAssoc(pos, name, code);
  76. if (0 == _tcsnicmp(name, m_strInput, len))
  77. {
  78. index = i;
  79. break;
  80. }
  81. i++;
  82. }
  83. if (index != -1)
  84. {
  85. m_Index = index;
  86. SetCurSel(m_Index);
  87. SetEditSel(4, len);
  88. }
  89. else
  90. {
  91. m_strInput.ReleaseBuffer(--len);
  92. MessageBeep(MB_ICONQUESTION);
  93. }
  94. }
  95. }
  96. else if (len > 2)
  97. {
  98. // try to find it in country names list
  99. index = -1;
  100. POSITION pos = m_map_name_code.GetStartPosition();
  101. while (pos != NULL)
  102. {
  103. CString name, code;
  104. m_map_name_code.GetNextAssoc(pos, name, code);
  105. if (0 == _tcsnicmp(name, m_strInput, len))
  106. {
  107. index = FindString(-1, code);
  108. break;
  109. }
  110. }
  111. if (index != -1)
  112. {
  113. m_Index = index;
  114. SetCurSel(m_Index);
  115. SetEditSel(4, 4+len);
  116. }
  117. else
  118. {
  119. m_strInput.ReleaseBuffer(--len);
  120. MessageBeep(MB_ICONQUESTION);
  121. }
  122. }
  123. else
  124. {
  125. // just remove selection
  126. SetEditSel(-1, 0);
  127. }
  128. return FALSE;
  129. }
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CCountryComboBox
  132. CCountryComboBox::CCountryComboBox()
  133. {
  134. }
  135. CCountryComboBox::~CCountryComboBox()
  136. {
  137. }
  138. #define IDC_COMBOEDIT 1001
  139. BOOL
  140. CCountryComboBox::SubclassDlgItem(UINT nID, CWnd * pParent)
  141. {
  142. return CComboBox::SubclassDlgItem(nID, pParent)
  143. && m_edit.SubclassDlgItem(IDC_COMBOEDIT, this);
  144. }
  145. BOOL
  146. CCountryComboBox::Init()
  147. {
  148. BOOL rc = FALSE;
  149. CString strData, strCode, strName, str;
  150. for (int i = IDS_COUNTRIES_FIRST;; i++)
  151. {
  152. if (!strData.LoadString(i))
  153. {
  154. break;
  155. }
  156. if (strData.IsEmpty())
  157. {
  158. rc = TRUE;
  159. break;
  160. }
  161. strCode = strData.Left(2);
  162. strName = strData.Right(strData.GetLength() - 2);
  163. str = strCode;
  164. str += _T(" (");
  165. str += strName;
  166. str += _T(")");
  167. if (CB_ERR == AddString(str))
  168. break;
  169. m_map_name_code.SetAt(strName, strCode);
  170. }
  171. return rc;
  172. }
  173. #define MAX_COUNTRY_NAME 64
  174. #define MAX_COUNTRY_CODE 3
  175. void CCountryComboBox::SetSelectedCountry(CString& country_code)
  176. {
  177. int index;
  178. if (country_code.IsEmpty())
  179. {
  180. TCHAR szCountryName[MAX_COUNTRY_NAME+1];
  181. VERIFY(GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,
  182. LOCALE_SCOUNTRY, szCountryName, MAX_COUNTRY_NAME));
  183. m_map_name_code.Lookup(szCountryName, country_code);
  184. }
  185. if (!country_code.IsEmpty() && CB_ERR != (index = FindString(-1, country_code)))
  186. {
  187. SetCurSel(index);
  188. }
  189. else
  190. SetCurSel(0);
  191. }
  192. void CCountryComboBox::GetSelectedCountry(CString& country_code)
  193. {
  194. CString str;
  195. GetLBText(GetCurSel(), str);
  196. country_code = str.Left(2);
  197. }
  198. BEGIN_MESSAGE_MAP(CCountryComboBox, CComboBox)
  199. //{{AFX_MSG_MAP(CCountryComboBox)
  200. //}}AFX_MSG_MAP
  201. END_MESSAGE_MAP()
  202. /////////////////////////////////////////////////////////////////////////////
  203. // CCountryComboBox message handlers