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.

275 lines
5.8 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 10
  175. void CCountryComboBox::SetSelectedCountry(CString& country_code)
  176. {
  177. int index;
  178. TCHAR szCountryName[MAX_COUNTRY_NAME+1];
  179. TCHAR sz3CharCountryCode[MAX_COUNTRY_CODE+1];
  180. int iRet = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME, sz3CharCountryCode, MAX_COUNTRY_CODE);
  181. iRet = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SCOUNTRY, szCountryName, MAX_COUNTRY_NAME);
  182. if (country_code.IsEmpty())
  183. {
  184. // Try to look it up from the country name
  185. // i know this is kind of weird since we can look it up from
  186. // the countrycode, but this code has been like this for a while
  187. // and i don't want to break anything
  188. m_map_name_code.Lookup(szCountryName, country_code);
  189. }
  190. if (!country_code.IsEmpty() && CB_ERR != (index = FindString(-1, country_code) ))
  191. {
  192. SetCurSel(index);
  193. }
  194. else
  195. {
  196. int len = 0;
  197. POSITION pos = NULL;
  198. CString name, code;
  199. //IISDebugOutput((_T("No Match for:%s\n"),szCountryName));
  200. // we didn't find a match
  201. // try looping thru the m_map_name_code
  202. // to find a matching country code
  203. index = -1;
  204. len = _tcslen(sz3CharCountryCode);
  205. pos = m_map_name_code.GetStartPosition();
  206. while (pos != NULL)
  207. {
  208. m_map_name_code.GetNextAssoc(pos, name, code);
  209. if (0 == _tcsnicmp(code, sz3CharCountryCode, len))
  210. {
  211. index = FindString(-1, code);
  212. break;
  213. }
  214. }
  215. if (index == -1)
  216. {
  217. // if we still didn't find it
  218. // then search thru the list and look for a similiar
  219. // looking country name
  220. index = -1;
  221. len = _tcslen(szCountryName);
  222. pos = m_map_name_code.GetStartPosition();
  223. while (pos != NULL)
  224. {
  225. m_map_name_code.GetNextAssoc(pos, name, code);
  226. if (0 == _tcsnicmp(name, szCountryName, len))
  227. {
  228. index = FindString(-1, code);
  229. break;
  230. }
  231. }
  232. }
  233. if (index != -1)
  234. {
  235. SetCurSel(index);
  236. }
  237. else
  238. {
  239. SetCurSel(0);
  240. }
  241. }
  242. }
  243. void CCountryComboBox::GetSelectedCountry(CString& country_code)
  244. {
  245. CString str;
  246. GetLBText(GetCurSel(), str);
  247. country_code = str.Left(2);
  248. }
  249. BEGIN_MESSAGE_MAP(CCountryComboBox, CComboBox)
  250. //{{AFX_MSG_MAP(CCountryComboBox)
  251. //}}AFX_MSG_MAP
  252. END_MESSAGE_MAP()
  253. /////////////////////////////////////////////////////////////////////////////
  254. // CCountryComboBox message handlers