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.

287 lines
7.9 KiB

  1. //
  2. // mvEdit.cpp : implementation file for multi-valued string edit dialog
  3. //
  4. #include "stdafx.h"
  5. #include "mvedit.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMultiValuedStringEdit dialog
  13. /*CMultiValuedStringEdit::CMultiValuedStringEdit(CWnd* pParent)
  14. : CDialog(CMultiValuedStringEdit::IDD, pParent)
  15. {
  16. m_nDlgTitle = 0;
  17. m_nText = 0;
  18. }
  19. */
  20. CMultiValuedStringEdit::CMultiValuedStringEdit(CWnd* pParent, int nDlgTitle, int nText, UINT uiStringLengthLimit)
  21. : CDialog(CMultiValuedStringEdit::IDD, pParent)
  22. {
  23. m_nDlgTitle = nDlgTitle;
  24. m_nText = nText;
  25. m_uiStringLengthLimit = ((0 == uiStringLengthLimit || MAX_PATH <= uiStringLengthLimit) ? (MAX_PATH - 1) : uiStringLengthLimit);
  26. }
  27. BEGIN_MESSAGE_MAP(CMultiValuedStringEdit, CDialog)
  28. //{{AFX_MSG_MAP(CMultiValuedStringEdit)
  29. ON_BN_CLICKED(IDC_MVSTRINGEDIT_ADD, OnAdd)
  30. ON_BN_CLICKED(IDC_MVSTRINGEDIT_REMOVE, OnRemove)
  31. ON_EN_CHANGE(IDC_MVSTRINGEDIT_STRING, OnString)
  32. ON_NOTIFY(LVN_ITEMCHANGED, IDC_MVSTRINGEDIT_LIST, OnList)
  33. ON_MESSAGE(WM_HELP, OnHelp)
  34. ON_MESSAGE(WM_CONTEXTMENU, OnContextHelp)
  35. //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37. BOOL CMultiValuedStringEdit::OnInitDialog()
  38. {
  39. CDialog::OnInitDialog();
  40. if (m_nDlgTitle)
  41. {
  42. CString strDlgTitle;
  43. strDlgTitle.LoadString(m_nDlgTitle);
  44. SetWindowText(strDlgTitle);
  45. }
  46. if (m_nText)
  47. {
  48. CString strText;
  49. strText.LoadString(m_nText);
  50. SetDlgItemText(IDC_MVSTRINGEDIT_TEXT, strText);
  51. }
  52. SendDlgItemMessage(IDC_MVSTRINGEDIT_STRING, EM_LIMITTEXT, m_uiStringLengthLimit, 0);
  53. HWND hwnd = GetDlgItem(IDC_MVSTRINGEDIT_LIST)->GetSafeHwnd();
  54. RECT rect = {0};
  55. ::GetWindowRect(hwnd, &rect);
  56. int nControlWidth = rect.right - rect.left;
  57. int nVScrollbarWidth = GetSystemMetrics(SM_CXVSCROLL);
  58. int nBorderWidth = GetSystemMetrics(SM_CXBORDER);
  59. int nControlNetWidth = nControlWidth - 4 * nBorderWidth;
  60. LVCOLUMN lvColumn = {0};
  61. lvColumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_WIDTH;
  62. lvColumn.fmt = LVCFMT_LEFT;
  63. lvColumn.iSubItem = 0;
  64. lvColumn.cx = nControlNetWidth;
  65. ListView_InsertColumn(hwnd, 0, &lvColumn);
  66. GetDlgItem(IDC_MVSTRINGEDIT_ADD)->EnableWindow(FALSE);
  67. GetDlgItem(IDC_MVSTRINGEDIT_REMOVE)->EnableWindow(FALSE);
  68. if (m_strValues.IsEmpty())
  69. return TRUE;
  70. CString strToken;
  71. int nIndex = 0;
  72. mystrtok(m_strValues, &nIndex, m_strSeparators, strToken);
  73. while (!strToken.IsEmpty())
  74. {
  75. strToken.TrimLeft();
  76. strToken.TrimRight();
  77. strToken.MakeLower();
  78. LVITEM lvItem = {0};
  79. lvItem.mask = LVIF_TEXT;
  80. lvItem.pszText = (LPTSTR)(LPCTSTR)strToken;
  81. ListView_InsertItem(hwnd, &lvItem);
  82. mystrtok(m_strValues, &nIndex, m_strSeparators, strToken);
  83. }
  84. return TRUE;
  85. }
  86. void CMultiValuedStringEdit::OnAdd()
  87. {
  88. CString str;
  89. GetDlgItemText(IDC_MVSTRINGEDIT_STRING, str);
  90. str.TrimLeft();
  91. str.TrimRight();
  92. if (!str.IsEmpty())
  93. {
  94. str.MakeLower();
  95. if (!_tcschr(str, *m_strSeparators))
  96. {
  97. LVFINDINFO lvInfo = {0};
  98. lvInfo.flags = LVFI_STRING;
  99. lvInfo.psz = str;
  100. HWND hwnd = GetDlgItem(IDC_MVSTRINGEDIT_LIST)->GetSafeHwnd();
  101. if (-1 == ListView_FindItem(hwnd, -1, &lvInfo))
  102. {
  103. LVITEM lvItem = {0};
  104. lvItem.mask = LVIF_TEXT;
  105. lvItem.pszText = (LPTSTR)(LPCTSTR)str;
  106. ListView_InsertItem(hwnd, &lvItem);
  107. }
  108. SetDlgItemText(IDC_MVSTRINGEDIT_STRING, _T(""));
  109. } else
  110. {
  111. DoErrMsgBox(m_hWnd, MB_OK, 0, IDS_MVSTRINGEDIT_STRING_INVALID, m_strSeparators);
  112. }
  113. }
  114. GetDlgItem(IDC_MVSTRINGEDIT_STRING)->SetFocus();
  115. }
  116. void CMultiValuedStringEdit::OnRemove()
  117. {
  118. HWND hwnd = GetDlgItem(IDC_MVSTRINGEDIT_LIST)->GetSafeHwnd();
  119. int nIndex = -1;
  120. while (-1 != (nIndex = ListView_GetNextItem(hwnd, -1, LVNI_SELECTED)))
  121. ListView_DeleteItem(hwnd, nIndex);
  122. }
  123. void CMultiValuedStringEdit::OnString()
  124. {
  125. int nLen = GetDlgItem(IDC_MVSTRINGEDIT_STRING)->GetWindowTextLength();
  126. GetDlgItem(IDC_MVSTRINGEDIT_ADD)->EnableWindow(0 < nLen);
  127. }
  128. void CMultiValuedStringEdit::OnList(NMHDR* /*pNMHDR*/, LRESULT* pResult)
  129. {
  130. HWND hwnd = GetDlgItem(IDC_MVSTRINGEDIT_LIST)->GetSafeHwnd();
  131. int nCount = ListView_GetSelectedCount(hwnd);
  132. GetDlgItem(IDC_MVSTRINGEDIT_REMOVE)->EnableWindow(nCount >= 1);
  133. *pResult = 0;
  134. }
  135. BOOL CMultiValuedStringEdit::OnHelp(WPARAM /*wParam*/, LPARAM lParam)
  136. {
  137. return DoHelp(lParam, HELP_DIALOG_TOPIC(IDD_MVSTRINGEDIT));
  138. }
  139. BOOL CMultiValuedStringEdit::OnContextHelp(WPARAM wParam, LPARAM /*lParam*/)
  140. {
  141. return DoContextHelp(wParam, HELP_DIALOG_TOPIC(IDD_MVSTRINGEDIT));
  142. }
  143. void CMultiValuedStringEdit::OnOK()
  144. {
  145. m_strValues.Empty();
  146. HRESULT hr = S_OK;
  147. HWND hwnd = GetDlgItem(IDC_MVSTRINGEDIT_LIST)->GetSafeHwnd();
  148. if (0 < ListView_GetItemCount(hwnd))
  149. {
  150. PTSTR pszText = (PTSTR)calloc(m_uiStringLengthLimit+1, sizeof(TCHAR));
  151. if (pszText)
  152. {
  153. int nIndex = -1;
  154. while (-1 != (nIndex = ListView_GetNextItem(hwnd, nIndex, LVNI_ALL)))
  155. {
  156. ListView_GetItemText(hwnd, nIndex, 0, pszText, m_uiStringLengthLimit+1);
  157. if (m_strValues.IsEmpty())
  158. {
  159. m_strValues = pszText;
  160. if (!m_strValues) { hr = E_OUTOFMEMORY; break; }
  161. } else
  162. {
  163. m_strValues += m_strSeparators;
  164. if (!m_strValues) { hr = E_OUTOFMEMORY; break; }
  165. m_strValues += pszText;
  166. if (!m_strValues) { hr = E_OUTOFMEMORY; break; }
  167. }
  168. }
  169. free(pszText);
  170. } else
  171. {
  172. hr = E_OUTOFMEMORY;
  173. }
  174. }
  175. if (FAILED(hr))
  176. DoErrMsgBox(m_hWnd, MB_OK, hr);
  177. else
  178. EndDialog(IDOK);
  179. }
  180. HRESULT CMultiValuedStringEdit::put_Strings(
  181. IN LPCTSTR i_pszValues,
  182. IN LPCTSTR i_pszSeparators
  183. )
  184. {
  185. if (!i_pszSeparators || 1 != lstrlen(i_pszSeparators))
  186. return E_INVALIDARG;
  187. m_strValues = i_pszValues;
  188. m_strSeparators = i_pszSeparators;
  189. m_strValues.MakeLower();
  190. m_strSeparators.MakeLower();
  191. return S_OK;
  192. }
  193. HRESULT CMultiValuedStringEdit::get_Strings
  194. (
  195. CString& o_strValues
  196. )
  197. {
  198. o_strValues = m_strValues;
  199. return S_OK;
  200. }
  201. /////////////////////////////////////////////////////////////////////////////
  202. // Helper routine to invoke the dialog.
  203. //
  204. // S_OK: io_str contains the new string
  205. // S_FALSE: dlg cancelled, or string unchanged
  206. // others: error occurred and reported
  207. //
  208. HRESULT InvokeMultiValuedStringEditDlg(
  209. IN CWnd* i_pParent,
  210. IN CString& io_str,
  211. IN LPCTSTR i_pszSeparators,
  212. IN int i_nDlgTitle,
  213. IN int i_nText,
  214. IN UINT i_uiStringLengthLimit
  215. )
  216. {
  217. CMultiValuedStringEdit editDlg(i_pParent, i_nDlgTitle, i_nText, i_uiStringLengthLimit);
  218. HRESULT hr = editDlg.put_Strings(io_str, i_pszSeparators);
  219. CThemeContextActivator activator;
  220. if (SUCCEEDED(hr) && IDOK == editDlg.DoModal())
  221. {
  222. CString str;
  223. hr = editDlg.get_Strings(str);
  224. if (SUCCEEDED(hr))
  225. {
  226. if (0 != io_str.CompareNoCase(str))
  227. {
  228. io_str = str;
  229. hr = S_OK;
  230. } else
  231. {
  232. hr = S_FALSE; // string unchanged
  233. }
  234. }
  235. }
  236. if (FAILED(hr))
  237. DoErrMsgBox(i_pParent->GetSafeHwnd(), MB_OK | MB_ICONSTOP, hr, IDS_MVSTRINGEDIT_ERROR);
  238. return hr;
  239. }