1
0

pageCredentials.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include "./pageCredentials.h"
  2. #include "./dataCredentials.h"
  3. #include "./common.h"
  4. #include "./loginGui.h"
  5. #include "../../winamp/commandLink.h"
  6. #include "../resource.h"
  7. #include <commctrl.h>
  8. static HRESULT CALLBACK LoginPageCredentials_CreateInstance(HWND hwnd, HWND hLoginbox, LoginPage **instance)
  9. {
  10. if (NULL == instance) return E_POINTER;
  11. if (NULL == hwnd || NULL == hLoginbox) return E_INVALIDARG;
  12. *instance = new LoginPageCredentials(hwnd, hLoginbox);
  13. if (NULL == instance) return E_OUTOFMEMORY;
  14. return S_OK;
  15. }
  16. LoginPageCredentials::LoginPageCredentials(HWND hwnd, HWND hLoginbox)
  17. : LoginPage(hwnd, hLoginbox), accountRecoverUrl(NULL), accountCreateUrl(NULL)
  18. {
  19. }
  20. LoginPageCredentials::~LoginPageCredentials()
  21. {
  22. LoginBox_FreeString(accountRecoverUrl);
  23. LoginBox_FreeString(accountCreateUrl);
  24. }
  25. HWND LoginPageCredentials::CreatePage(HWND hLoginbox, HWND hParent)
  26. {
  27. return LoginPage::CreatePage(hLoginbox, MAKEINTRESOURCE(IDD_PAGE_CREDENTIALS),
  28. hParent, NULL, LoginPageCredentials_CreateInstance);
  29. }
  30. void LoginPageCredentials::UpdateLayout(BOOL fRedraw)
  31. {
  32. LoginPage::UpdateLayout(fRedraw);
  33. RECT pageRect;
  34. if (FALSE == GetPageRect(&pageRect))
  35. return;
  36. UINT flags = SWP_NOACTIVATE | SWP_NOZORDER;
  37. if (FALSE == fRedraw) flags |= SWP_NOREDRAW;
  38. const INT szControls[] = { IDC_USERNAME_LABEL, IDC_USERNAME, IDC_PASSWORD_LABEL, IDC_PASSWORD};
  39. const INT szLinks[] = { IDC_CREATE_ACCOUNT, IDC_RECOVER_ACCOUNT };
  40. INT baseunitX, baseunitY;
  41. if (FALSE == LoginBox_GetWindowBaseUnits(hwnd, &baseunitX, &baseunitY))
  42. {
  43. baseunitX = 6;
  44. baseunitY = 13;
  45. }
  46. HDWP hdwp = BeginDeferWindowPos(ARRAYSIZE(szControls) + ARRAYSIZE(szLinks));
  47. HWND hControl;
  48. INT cx, cy, x, y;
  49. INT editWidth = 0;
  50. INT column1Width = 0;
  51. INT column2Width = 0;
  52. INT columnSpace = MulDiv(7, baseunitX, 4);
  53. SIZE sizeLink;
  54. RECT linkMargins;
  55. // column 1 width
  56. for (INT i = 0; i < ARRAYSIZE(szControls); i++)
  57. {
  58. hControl = GetDlgItem(hwnd, szControls[i]);
  59. if (NULL == hControl) continue;
  60. switch(szControls[i])
  61. {
  62. case IDC_USERNAME_LABEL:
  63. case IDC_PASSWORD_LABEL:
  64. if (FALSE != (LoginBox_GetWindowTextSize(hControl, pageRect.right - pageRect.left, &cx, &cy)) &&
  65. cx > column1Width)
  66. {
  67. column1Width = cx;
  68. }
  69. break;
  70. case IDC_USERNAME:
  71. case IDC_PASSWORD:
  72. if (0 == editWidth)
  73. {
  74. HDC hdc = GetDCEx(hControl, NULL, DCX_CACHE | DCX_NORESETATTRS);
  75. if (NULL != hdc)
  76. {
  77. HFONT fontControl = (HFONT)SendMessage(hControl, WM_GETFONT, 0, 0L);
  78. HFONT fontOrig = (HFONT)SelectObject(hdc, fontControl);
  79. editWidth = LoginBox_GetAveStrWidth(hdc, 28);
  80. SelectObject(hdc, fontOrig);
  81. ReleaseDC(hControl, hdc);
  82. }
  83. if (0 != editWidth)
  84. {
  85. LRESULT editMargins = SendMessage(hControl, EM_GETMARGINS, 0,0L);
  86. editWidth += (LOWORD(editMargins) + HIWORD(editMargins));
  87. }
  88. }
  89. if (editWidth > column1Width)
  90. column1Width = editWidth;
  91. break;
  92. }
  93. }
  94. // column 2 width
  95. for (INT i = 0; i < ARRAYSIZE(szLinks); i++)
  96. {
  97. hControl = GetDlgItem(hwnd, szLinks[i]);
  98. if (NULL != hControl &&
  99. 0 != (WS_VISIBLE & GetWindowStyle(hControl)) &&
  100. FALSE != CommandLink_GetIdealSize(hControl, &sizeLink))
  101. {
  102. if (FALSE != CommandLink_GetMargins(hControl, &linkMargins))
  103. sizeLink.cx -= linkMargins.right;
  104. if (column2Width < sizeLink.cx)
  105. column2Width = sizeLink.cx;
  106. }
  107. }
  108. BOOL multiColumnLayout;
  109. if (column2Width > 0 &&
  110. (column1Width + columnSpace + column2Width) <= (pageRect.right - pageRect.left))
  111. {
  112. multiColumnLayout = TRUE;
  113. }
  114. else
  115. {
  116. multiColumnLayout = FALSE;
  117. }
  118. INT nextTop = 0;
  119. if (column2Width != 0)
  120. {
  121. if(FALSE == multiColumnLayout)
  122. nextTop = pageRect.top - GetTitleSpacing()/2;
  123. else
  124. {
  125. nextTop = pageRect.top;
  126. HWND hLabel = GetDlgItem(hwnd, IDC_USERNAME_LABEL);
  127. INT offsetY = (NULL != hLabel) ? LoginBox_GetWindowTextHeight(hLabel, 0) : 0;
  128. if (0 == offsetY) offsetY = baseunitY;
  129. nextTop += (offsetY - 1);
  130. }
  131. for (INT i = 0; i < ARRAYSIZE(szLinks); i++)
  132. {
  133. hControl = GetDlgItem(hwnd, szLinks[i]);
  134. if (NULL == hControl || 0 == (WS_VISIBLE & GetWindowStyle(hControl)))
  135. continue;
  136. if (FALSE == CommandLink_GetIdealSize(hControl, &sizeLink))
  137. ZeroMemory(&sizeLink, sizeof(SIZE));
  138. x = pageRect.right - sizeLink.cx;
  139. if (x < pageRect.left) x = pageRect.left;
  140. if (FALSE != CommandLink_GetMargins(hControl, &linkMargins))
  141. x += linkMargins.right;
  142. if (i == 0)
  143. nextTop -= linkMargins.top;
  144. hdwp = DeferWindowPos(hdwp, hControl, NULL, x, nextTop, sizeLink.cx, sizeLink.cy, flags);
  145. if (NULL == hdwp) break;
  146. nextTop += sizeLink.cy;
  147. if (i != (ARRAYSIZE(szLinks) -1))
  148. nextTop += MulDiv(1, baseunitY, 8);
  149. }
  150. }
  151. if (FALSE != multiColumnLayout || 0 == column2Width)
  152. nextTop = pageRect.top;
  153. else
  154. nextTop += MulDiv(2, baseunitY, 8);
  155. for (INT i = 0; i < ARRAYSIZE(szControls); i++)
  156. {
  157. hControl = GetDlgItem(hwnd, szControls[i]);
  158. if (NULL == hControl) continue;
  159. switch(szControls[i])
  160. {
  161. case IDC_USERNAME_LABEL:
  162. case IDC_PASSWORD_LABEL:
  163. x = pageRect.left;
  164. y = nextTop;
  165. LoginBox_GetWindowTextSize(hControl, pageRect.right - pageRect.left, &cx, &cy);
  166. nextTop += (cy + MulDiv(2, baseunitY, 8));
  167. if (cx > column1Width)
  168. cx = column1Width;
  169. break;
  170. case IDC_USERNAME:
  171. case IDC_PASSWORD:
  172. x = pageRect.left;
  173. y = nextTop;
  174. cy = LoginBox_GetWindowTextHeight(hControl, 0);
  175. {
  176. RECT r1, r2;
  177. GetWindowRect(hControl, &r1);
  178. SendMessage(hControl, EM_GETRECT, 0, (LPARAM)&r2);
  179. INT t = (r1.bottom - r1.top) - (r2.bottom - r2.top);
  180. cy += t;
  181. }
  182. nextTop += (cy + MulDiv(4, baseunitY, 8));
  183. cx = column1Width;
  184. if ((x + cx) > pageRect.right)
  185. cx = pageRect.right - x;
  186. break;
  187. }
  188. hdwp = DeferWindowPos(hdwp, hControl, NULL, x, y, cx, cy, flags);
  189. if (NULL == hdwp) break;
  190. }
  191. if (NULL != hdwp)
  192. EndDeferWindowPos(hdwp);
  193. }
  194. BOOL LoginPageCredentials::OnInitDialog(HWND hFocus, LPARAM param)
  195. {
  196. HWND hControl;
  197. const INT szLinks[] = { IDC_CREATE_ACCOUNT, IDC_RECOVER_ACCOUNT };
  198. for (INT i = 0; i < ARRAYSIZE(szLinks); i++)
  199. {
  200. hControl = GetDlgItem(hwnd, szLinks[i]);
  201. if (NULL == hControl) continue;
  202. RECT linkMargins;
  203. SetRect(&linkMargins, 2, 0, 2, 2);
  204. MapDialogRect(hwnd, &linkMargins);
  205. CommandLink_SetMargins(hControl, &linkMargins);
  206. }
  207. HFONT fontLabel = NULL, fontEdit = NULL;
  208. LoginGuiObject *loginGui;
  209. if (SUCCEEDED(LoginGuiObject::QueryInstance(&loginGui)))
  210. {
  211. fontLabel = loginGui->GetTextFont();
  212. fontEdit = loginGui->GetEditorFont();
  213. loginGui->Release();
  214. }
  215. if (NULL != fontLabel)
  216. {
  217. const INT szLabels[] = { IDC_USERNAME_LABEL, IDC_PASSWORD_LABEL};
  218. for (INT i = 0; i < ARRAYSIZE(szLabels); i++)
  219. {
  220. hControl = GetDlgItem(hwnd, szLabels[i]);
  221. if (NULL != hControl)
  222. SendMessage(hControl, WM_SETFONT, (WPARAM)fontLabel, 0L);
  223. }
  224. }
  225. if (NULL != fontEdit)
  226. {
  227. const INT szEdits[] = { IDC_USERNAME, IDC_PASSWORD};
  228. for (INT i = 0; i < ARRAYSIZE(szEdits); i++)
  229. {
  230. hControl = GetDlgItem(hwnd, szEdits[i]);
  231. if (NULL != hControl)
  232. SendMessage(hControl, WM_SETFONT, (WPARAM)fontEdit, 0L);
  233. }
  234. }
  235. LoginPage::OnInitDialog(hFocus, param);
  236. return FALSE;
  237. }
  238. BOOL LoginPageCredentials::OnNotify(UINT controlId, const NMHDR *pnmh)
  239. {
  240. switch(controlId)
  241. {
  242. case IDC_CREATE_ACCOUNT:
  243. if (pnmh->code == NM_CLICK && NULL != accountCreateUrl && L'\0' != *accountCreateUrl)
  244. LoginBox_OpenUrl(hwnd, accountCreateUrl, TRUE);
  245. return TRUE;
  246. case IDC_RECOVER_ACCOUNT:
  247. if (pnmh->code == NM_CLICK && NULL != accountRecoverUrl && L'\0' != *accountRecoverUrl)
  248. LoginBox_OpenUrl(hwnd, accountRecoverUrl, TRUE);
  249. return TRUE;
  250. }
  251. return LoginPage::OnNotify(controlId, pnmh);
  252. }
  253. BOOL LoginPageCredentials::OnGetLoginData(LoginData **ppLoginData)
  254. {
  255. if (NULL == ppLoginData)
  256. return FALSE;
  257. HWND hEdit;
  258. LPWSTR username, password;
  259. hEdit = GetDlgItem(hwnd, IDC_USERNAME);
  260. if (NULL == hEdit || FAILED(LoginBox_GetWindowText(hEdit, &username, NULL)))
  261. username = NULL;
  262. hEdit = GetDlgItem(hwnd, IDC_PASSWORD);
  263. if (NULL == hEdit || FAILED(LoginBox_GetWindowText(hEdit, &password, NULL)))
  264. password = NULL;
  265. HRESULT hr = LoginDataCredentials::CreateInstance(NULL, hwnd, hLoginbox,
  266. username, password, (LoginDataCredentials**)ppLoginData);
  267. LoginBox_FreeStringSecure(username);
  268. LoginBox_FreeStringSecure(password);
  269. return SUCCEEDED(hr);
  270. }
  271. void LoginPageCredentials::OnSetAccountRecoverUrl(LPCWSTR pszUrl)
  272. {
  273. LoginBox_FreeString(accountRecoverUrl);
  274. accountRecoverUrl = LoginBox_CopyString(pszUrl);
  275. HWND hLink = GetDlgItem(hwnd, IDC_RECOVER_ACCOUNT);
  276. if (NULL != hLink)
  277. {
  278. INT showWindow = (NULL != accountRecoverUrl && S_OK == IsValidURL(NULL, accountRecoverUrl, 0)) ? SW_SHOWNA : SW_HIDE;
  279. ShowWindow(hLink, showWindow);
  280. }
  281. }
  282. void LoginPageCredentials::OnSetAccountCreateUrl(LPCWSTR pszUrl)
  283. {
  284. LoginBox_FreeString(accountCreateUrl);
  285. accountCreateUrl = LoginBox_CopyString(pszUrl);
  286. HWND hLink = GetDlgItem(hwnd, IDC_CREATE_ACCOUNT);
  287. if (NULL != hLink)
  288. {
  289. INT showWindow = (NULL != accountCreateUrl && S_OK == IsValidURL(NULL, accountCreateUrl, 0)) ? SW_SHOWNA : SW_HIDE;
  290. ShowWindow(hLink, showWindow);
  291. }
  292. }
  293. void LoginPageCredentials::OnSetUsernameLabel(LPCWSTR pszLabel)
  294. {
  295. SetLabelText(IDC_USERNAME_LABEL, pszLabel);
  296. }
  297. void LoginPageCredentials::OnSetPasswordLabel(LPCWSTR pszLabel)
  298. {
  299. SetLabelText(IDC_PASSWORD_LABEL, pszLabel);
  300. }
  301. BOOL LoginPageCredentials::OnSetUsername(LPCWSTR pszUsername)
  302. {
  303. HWND hEdit = GetDlgItem(hwnd, IDC_USERNAME);
  304. if (NULL == hEdit) return FALSE;
  305. BOOL result = (BOOL)SNDMSG(hEdit, WM_SETTEXT, 0, (LPARAM)pszUsername);
  306. if (FALSE != result)
  307. {
  308. SNDMSG(hEdit, EM_SETSEL, 0, -1);
  309. }
  310. return result;
  311. }
  312. BOOL LoginPageCredentials::OnSetPassword(LPCWSTR pszPassword)
  313. {
  314. HWND hEdit = GetDlgItem(hwnd, IDC_PASSWORD);
  315. if (NULL == hEdit) return FALSE;
  316. BOOL result = (BOOL)SNDMSG(hEdit, WM_SETTEXT, 0, (LPARAM)pszPassword);
  317. if (FALSE != result)
  318. {
  319. SNDMSG(hEdit, EM_SETSEL, 0, -1);
  320. }
  321. return result;
  322. }
  323. HWND LoginPageCredentials::OnGetFirstItem()
  324. {
  325. HWND hEdit = GetDlgItem(hwnd, IDC_USERNAME);
  326. if (NULL != hEdit && (WS_VISIBLE == ((WS_VISIBLE | WS_DISABLED) & GetWindowStyle(hEdit))))
  327. return hEdit;
  328. return LoginPage::OnGetFirstItem();
  329. }
  330. INT_PTR LoginPageCredentials::DialogProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  331. {
  332. switch(uMsg)
  333. {
  334. case NLPCM_SETACCOUNTRECOVERURL: OnSetAccountRecoverUrl((LPCWSTR)lParam); return TRUE;
  335. case NLPCM_SETACCOUNTCREATEURL: OnSetAccountCreateUrl((LPCWSTR)lParam); return TRUE;
  336. case NLPCM_SETUSERNAMELABEL: OnSetUsernameLabel((LPCWSTR)lParam); return TRUE;
  337. case NLPCM_SETPASSWORDLABEL: OnSetPasswordLabel((LPCWSTR)lParam); return TRUE;
  338. }
  339. return __super::DialogProc(uMsg, wParam, lParam);
  340. }