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.

300 lines
6.5 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. {
  18. CEdit::OnChar(nChar, nRepCnt, nFlags);
  19. }
  20. }
  21. BOOL CComboEdit::SubclassDlgItem(UINT nID, CCountryComboBox * pParent)
  22. {
  23. ASSERT(pParent != NULL);
  24. m_pParent = pParent;
  25. return CEdit::SubclassDlgItem(nID, pParent);
  26. }
  27. BOOL CCountryComboBox::OnEditChar(UINT nChar)
  28. {
  29. int index;
  30. int len = m_strInput.GetLength();
  31. if ( ((nChar == VK_ESCAPE) || (nChar == VK_RETURN) || (nChar == VK_TAB)) &&
  32. GetDroppedState() )
  33. {
  34. // hide the dropdown
  35. ShowDropDown(FALSE);
  36. }
  37. if (nChar == VK_RETURN)
  38. {
  39. // Check if there is something selected...
  40. index = GetCurSel();
  41. if (index != -1)
  42. {
  43. m_Index = index;
  44. SetCurSel(m_Index);
  45. SetEditSel(0, m_strInput.GetLength());
  46. return TRUE;
  47. }
  48. }
  49. if (nChar == VK_ESCAPE)
  50. {
  51. if (len == 0)
  52. {
  53. MessageBeep(MB_ICONQUESTION);
  54. return FALSE;
  55. }
  56. m_strInput.Empty();
  57. len = 0;
  58. return FALSE;
  59. }
  60. else if (nChar == VK_BACK)
  61. {
  62. if (len == 0)
  63. {
  64. MessageBeep(MB_ICONQUESTION);
  65. return FALSE;
  66. }
  67. m_strInput.ReleaseBuffer(--len);
  68. }
  69. else if (_istalpha((TCHAR) nChar) || VK_SPACE == nChar)
  70. {
  71. m_strInput += (TCHAR)nChar;
  72. len++;
  73. }
  74. else
  75. {
  76. MessageBeep(MB_ICONQUESTION);
  77. return FALSE;
  78. }
  79. if (len > 0 && len <= 2)
  80. {
  81. if (CB_ERR != (index = FindString(-1, m_strInput)))
  82. {
  83. m_Index = index;
  84. SetCurSel(m_Index);
  85. SetEditSel(0, m_strInput.GetLength());
  86. }
  87. else
  88. {
  89. // try to find it in country names list
  90. index = -1;
  91. POSITION pos = m_map_name_code.GetStartPosition();
  92. int i = 0;
  93. while (pos != NULL)
  94. {
  95. CString name, code;
  96. m_map_name_code.GetNextAssoc(pos, name, code);
  97. if (0 == _tcsnicmp(name, m_strInput, len))
  98. {
  99. index = i;
  100. break;
  101. }
  102. i++;
  103. }
  104. if (index != -1)
  105. {
  106. m_Index = index;
  107. SetCurSel(m_Index);
  108. SetEditSel(4, len);
  109. }
  110. else
  111. {
  112. m_strInput.ReleaseBuffer(--len);
  113. MessageBeep(MB_ICONQUESTION);
  114. }
  115. }
  116. }
  117. else if (len > 2)
  118. {
  119. // try to find it in country names list
  120. index = -1;
  121. POSITION pos = m_map_name_code.GetStartPosition();
  122. while (pos != NULL)
  123. {
  124. CString name, code;
  125. m_map_name_code.GetNextAssoc(pos, name, code);
  126. if (0 == _tcsnicmp(name, m_strInput, len))
  127. {
  128. index = FindString(-1, code);
  129. break;
  130. }
  131. }
  132. if (index != -1)
  133. {
  134. m_Index = index;
  135. SetCurSel(m_Index);
  136. SetEditSel(4, 4+len);
  137. }
  138. else
  139. {
  140. m_strInput.ReleaseBuffer(--len);
  141. MessageBeep(MB_ICONQUESTION);
  142. }
  143. }
  144. else
  145. {
  146. // just remove selection
  147. SetEditSel(-1, 0);
  148. }
  149. return FALSE;
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CCountryComboBox
  153. CCountryComboBox::CCountryComboBox()
  154. {
  155. }
  156. CCountryComboBox::~CCountryComboBox()
  157. {
  158. }
  159. #define IDC_COMBOEDIT 1001
  160. BOOL
  161. CCountryComboBox::SubclassDlgItem(UINT nID, CWnd * pParent)
  162. {
  163. return CComboBox::SubclassDlgItem(nID, pParent)
  164. && m_edit.SubclassDlgItem(IDC_COMBOEDIT, this);
  165. }
  166. BOOL
  167. CCountryComboBox::Init()
  168. {
  169. BOOL rc = FALSE;
  170. CString strData, strCode, strName, str;
  171. for (int i = IDS_COUNTRIES_FIRST;; i++)
  172. {
  173. if (!strData.LoadString(i))
  174. {
  175. break;
  176. }
  177. if (strData.IsEmpty())
  178. {
  179. rc = TRUE;
  180. break;
  181. }
  182. strCode = strData.Left(2);
  183. strName = strData.Right(strData.GetLength() - 2);
  184. str = strCode;
  185. str += _T(" (");
  186. str += strName;
  187. str += _T(")");
  188. if (CB_ERR == AddString(str))
  189. break;
  190. m_map_name_code.SetAt(strName, strCode);
  191. }
  192. return rc;
  193. }
  194. #define MAX_COUNTRY_NAME 64
  195. #define MAX_COUNTRY_CODE 10
  196. void CCountryComboBox::SetSelectedCountry(CString& country_code)
  197. {
  198. int index;
  199. TCHAR szCountryName[MAX_COUNTRY_NAME+1];
  200. TCHAR sz3CharCountryCode[MAX_COUNTRY_CODE+1];
  201. int iRet = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME, sz3CharCountryCode, MAX_COUNTRY_CODE);
  202. iRet = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SCOUNTRY, szCountryName, MAX_COUNTRY_NAME);
  203. if (country_code.IsEmpty())
  204. {
  205. // Try to look it up from the country name
  206. // i know this is kind of weird since we can look it up from
  207. // the countrycode, but this code has been like this for a while
  208. // and i don't want to break anything
  209. m_map_name_code.Lookup(szCountryName, country_code);
  210. }
  211. if (!country_code.IsEmpty() && CB_ERR != (index = FindString(-1, country_code) ))
  212. {
  213. SetCurSel(index);
  214. }
  215. else
  216. {
  217. int len = 0;
  218. POSITION pos = NULL;
  219. CString name, code;
  220. //IISDebugOutput((_T("No Match for:%s\n"),szCountryName));
  221. // we didn't find a match
  222. // try looping thru the m_map_name_code
  223. // to find a matching country code
  224. index = -1;
  225. len = _tcslen(sz3CharCountryCode);
  226. pos = m_map_name_code.GetStartPosition();
  227. while (pos != NULL)
  228. {
  229. m_map_name_code.GetNextAssoc(pos, name, code);
  230. if (0 == _tcsnicmp(code, sz3CharCountryCode, len))
  231. {
  232. index = FindString(-1, code);
  233. break;
  234. }
  235. }
  236. if (index == -1)
  237. {
  238. // if we still didn't find it
  239. // then search thru the list and look for a similiar
  240. // looking country name
  241. index = -1;
  242. len = _tcslen(szCountryName);
  243. pos = m_map_name_code.GetStartPosition();
  244. while (pos != NULL)
  245. {
  246. m_map_name_code.GetNextAssoc(pos, name, code);
  247. if (0 == _tcsnicmp(name, szCountryName, len))
  248. {
  249. index = FindString(-1, code);
  250. break;
  251. }
  252. }
  253. }
  254. if (index != -1)
  255. {
  256. SetCurSel(index);
  257. }
  258. else
  259. {
  260. SetCurSel(0);
  261. }
  262. }
  263. }
  264. void CCountryComboBox::GetSelectedCountry(CString& country_code)
  265. {
  266. CString str;
  267. GetLBText(GetCurSel(), str);
  268. country_code = str.Left(2);
  269. }
  270. BEGIN_MESSAGE_MAP(CCountryComboBox, CComboBox)
  271. //{{AFX_MSG_MAP(CCountryComboBox)
  272. //}}AFX_MSG_MAP
  273. END_MESSAGE_MAP()
  274. /////////////////////////////////////////////////////////////////////////////
  275. // CCountryComboBox message handlers