1
0

smtpDlg.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include ".\smtpdlg.h"
  2. #include ".\resource.h"
  3. #include ".\settings.h"
  4. #include <strsafe.h>
  5. extern Settings settings;
  6. void UpdateAuth(HWND hwndDlg, BOOL enabled)
  7. {
  8. EnableWindow(GetDlgItem(hwndDlg, IDC_LBL_USER), enabled);
  9. EnableWindow(GetDlgItem(hwndDlg, IDC_EDT_USER), enabled);
  10. EnableWindow(GetDlgItem(hwndDlg, IDC_LBL_PWD), enabled);
  11. EnableWindow(GetDlgItem(hwndDlg, IDC_EDT_PWD), enabled);
  12. }
  13. BOOL CALLBACK smtpDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  14. {
  15. switch(uMsg)
  16. {
  17. case WM_INITDIALOG:
  18. {
  19. wchar_t num[16] = {0};
  20. CenterDialog(hwndDlg);
  21. SetWindowText(GetDlgItem(hwndDlg, IDC_EDT_SERVER), settings.smtpServer);
  22. SetWindowText(GetDlgItem(hwndDlg, IDC_EDT_USER), settings.smtpUser);
  23. SetWindowText(GetDlgItem(hwndDlg, IDC_EDT_PWD), settings.smtpPwd);
  24. SetWindowText(GetDlgItem(hwndDlg, IDC_EDT_PORT), _itow(settings.smtpPort, num, 10));
  25. SetWindowText(GetDlgItem(hwndDlg, IDC_EDT_ADDRESS), settings.smtpAddress);
  26. CheckDlgButton(hwndDlg, IDC_CHK_AUTH, settings.smtpAuth);
  27. UpdateAuth(hwndDlg, settings.smtpAuth);
  28. break;
  29. }
  30. case WM_DESTROY:
  31. {
  32. wchar_t buf[1024] = {0};
  33. int len;
  34. if (settings.smtpServer) free(settings.smtpServer);
  35. settings.smtpServer = NULL;
  36. len = GetWindowText(GetDlgItem(hwndDlg, IDC_EDT_SERVER), buf, 1024);
  37. if (len)
  38. {
  39. settings.smtpServer = (wchar_t*)malloc((len + 1)*2);
  40. StringCchCopy(settings.smtpServer, len+1, buf);
  41. }
  42. len = GetWindowText(GetDlgItem(hwndDlg, IDC_EDT_PORT), buf, 1024);
  43. if (len) settings.smtpPort = _wtoi(buf);
  44. if (settings.smtpUser) free(settings.smtpUser);
  45. settings.smtpUser = NULL;
  46. len = GetWindowText(GetDlgItem(hwndDlg, IDC_EDT_USER), buf, 1024);
  47. if (len)
  48. {
  49. settings.smtpUser = (wchar_t*)malloc((len + 1)*2);
  50. StringCchCopy(settings.smtpUser, len+1, buf);
  51. }
  52. if (settings.smtpPwd) free(settings.smtpPwd);
  53. settings.smtpPwd = NULL;
  54. len = GetWindowText(GetDlgItem(hwndDlg, IDC_EDT_PWD), buf, 1024);
  55. if (len)
  56. {
  57. settings.smtpPwd = (wchar_t*)malloc((len + 1)*2);
  58. StringCchCopy(settings.smtpPwd, len+1, buf);
  59. }
  60. if (settings.smtpAddress) free(settings.smtpAddress);
  61. settings.smtpAddress = NULL;
  62. len = GetWindowText(GetDlgItem(hwndDlg, IDC_EDT_ADDRESS), buf, 1024);
  63. if (len)
  64. {
  65. settings.smtpAddress = (wchar_t*)malloc((len + 1)*2);
  66. StringCchCopy(settings.smtpAddress, len+1, buf);
  67. }
  68. settings.smtpAuth = (SendMessage(GetDlgItem(hwndDlg, IDC_CHK_AUTH), BM_GETCHECK, 0,0) == BST_CHECKED);
  69. settings.Save();
  70. break;
  71. }
  72. case WM_COMMAND:
  73. switch(LOWORD(wParam))
  74. {
  75. case IDC_CHK_AUTH:
  76. UpdateAuth(hwndDlg, (SendMessage((HWND) lParam, BM_GETCHECK, 0,0) == BST_CHECKED));
  77. break;
  78. case IDCANCEL:
  79. EndDialog(hwndDlg, 0);
  80. break;
  81. }
  82. break;
  83. }
  84. return FALSE;
  85. }
  86. void CenterDialog(HWND hwndDlg)
  87. {
  88. HWND hwndOwner;
  89. RECT rc, rcDlg, rcOwner;
  90. if ((hwndOwner = GetParent(hwndDlg)) == NULL)
  91. {
  92. hwndOwner = GetDesktopWindow();
  93. }
  94. GetWindowRect(hwndOwner, &rcOwner);
  95. GetWindowRect(hwndDlg, &rcDlg);
  96. CopyRect(&rc, &rcOwner);
  97. // Offset the owner and dialog box rectangles so that
  98. // right and bottom values represent the width and
  99. // height, and then offset the owner again to discard
  100. // space taken up by the dialog box.
  101. OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
  102. OffsetRect(&rc, -rc.left, -rc.top);
  103. OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
  104. // The new position is the sum of half the remaining
  105. // space and the owner's original position.
  106. SetWindowPos(hwndDlg,
  107. HWND_TOP,
  108. rcOwner.left + (rc.right / 2),
  109. rcOwner.top + (rc.bottom / 2),
  110. 0, 0, // ignores size arguments
  111. SWP_NOSIZE);
  112. }