loginNotifier.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. #define OEMRESOURCE
  2. #include "./loginNotifier.h"
  3. #include "./common.h"
  4. #include "./loginGui.h"
  5. #include "../api.h"
  6. #include "../resource.h"
  7. #include "../api_auth.h"
  8. #include <strsafe.h>
  9. #define NWC_LOGINNOTIFIER L"NullsoftLoginNotifier"
  10. #define GRADIENT_LEFT 30
  11. #define GRADIENT_RIGHT 10
  12. #define SPACING_TOP 2
  13. #define SPACING_BOTTOM 2
  14. typedef struct __LOGINNOTIFIER
  15. {
  16. UINT flags;
  17. HBITMAP image;
  18. INT type;
  19. LPWSTR text;
  20. HFONT font;
  21. INT textHeight;
  22. INT aveCharWidth;
  23. COLORREF rgbBack;
  24. COLORREF rgbText;
  25. } LOGINNOTIFIER;
  26. #define GetNotifier(__hwnd) ((LOGINNOTIFIER*)(LONG_PTR)(LONGX86)GetWindowLongPtr((__hwnd), 0))
  27. static LRESULT WINAPI LoginNotifier_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  28. static BOOL LoginNotifier_RegisterClass(HINSTANCE hInstance)
  29. {
  30. WNDCLASSW wc;
  31. if (FALSE != GetClassInfo(hInstance, NWC_LOGINNOTIFIER, &wc))
  32. return TRUE;
  33. ZeroMemory(&wc, sizeof(wc));
  34. wc.lpszClassName = NWC_LOGINNOTIFIER;
  35. wc.lpfnWndProc = LoginNotifier_WindowProc;
  36. wc.style = CS_PARENTDC;
  37. wc.cbWndExtra = sizeof(LOGINNOTIFIER*);
  38. wc.hInstance = hInstance;
  39. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  40. return ( 0 != RegisterClassW(&wc));
  41. }
  42. HWND LoginNotifier_CreateWindow(UINT styleEx, UINT style, INT x, INT y, INT cx, INT cy, HWND hParent, INT controlId)
  43. {
  44. if (FALSE == LoginNotifier_RegisterClass(WASABI_API_ORIG_HINST))
  45. return NULL;
  46. return CreateWindowEx(styleEx, NWC_LOGINNOTIFIER, NULL, WS_CHILD | style, x, y, cx, cy,
  47. hParent, (HMENU)(INT_PTR)controlId, WASABI_API_ORIG_HINST, NULL);
  48. }
  49. static HBITMAP LoginNotifier_CreateTypeImage(HDC hdc, INT type, INT height)
  50. {
  51. INT iconIndex;
  52. COLORREF rgbBack = RGB(255, 255, 255);
  53. COLORREF rgbAlert;
  54. switch(type)
  55. {
  56. case NLNTYPE_INFORMATION:
  57. iconIndex = LoginGuiObject::iconInfo;
  58. rgbAlert = RGB(209, 222, 254);
  59. break;
  60. case NLNTYPE_WARNING:
  61. iconIndex = LoginGuiObject::iconWarning;
  62. rgbAlert = RGB(254, 241, 148);
  63. break;
  64. case NLNTYPE_ERROR:
  65. iconIndex = LoginGuiObject::iconError;
  66. rgbAlert = RGB(225, 105, 105);
  67. break;
  68. case NLNTYPE_QUESTION:
  69. iconIndex = LoginGuiObject::iconQuestion;
  70. rgbAlert = RGB(209, 222, 254);
  71. break;
  72. default:
  73. iconIndex = LoginGuiObject::iconNone;
  74. rgbAlert = GetSysColor(COLOR_3DLIGHT);
  75. break;
  76. }
  77. RECT iconRect;
  78. HBITMAP bitmapIcons = NULL;
  79. if (LoginGuiObject::iconNone != iconIndex)
  80. {
  81. LoginGuiObject *loginGui;
  82. if (SUCCEEDED(LoginGuiObject::QueryInstance(&loginGui)))
  83. {
  84. bitmapIcons = loginGui->GetIcon(iconIndex, &iconRect);
  85. loginGui->Release();
  86. }
  87. }
  88. if (NULL == bitmapIcons)
  89. SetRectEmpty(&iconRect);
  90. INT iconWidth = iconRect.right - iconRect.left;
  91. INT iconHeight = iconRect.bottom - iconRect.top;
  92. INT width = GRADIENT_LEFT + GRADIENT_RIGHT;
  93. width += iconWidth;
  94. HBITMAP bitmapDst = NULL;
  95. HDC contextDst = CreateCompatibleDC(hdc);
  96. if (NULL != contextDst)
  97. {
  98. bitmapDst = CreateCompatibleBitmap(hdc, width, height);
  99. if (NULL != bitmapDst)
  100. {
  101. HBITMAP bitmapDstOrig = (HBITMAP)SelectObject(contextDst, bitmapDst);
  102. TRIVERTEX vertex[] =
  103. {
  104. { 0, 0, GetRValue(rgbBack) << 8, GetGValue(rgbBack) << 8, GetBValue(rgbBack) << 8, 0x0000 },
  105. { GRADIENT_LEFT, height, GetRValue(rgbAlert) << 8, GetGValue(rgbAlert) << 8, GetBValue(rgbAlert) << 8, 0x0000 },
  106. };
  107. GRADIENT_RECT gradientRect;
  108. gradientRect.UpperLeft = 0;
  109. gradientRect.LowerRight = 1;
  110. RECT fillRect;
  111. SetRect(&fillRect, 0, 0, width, height);
  112. if (FALSE != GdiGradientFill(contextDst, vertex, ARRAYSIZE(vertex), &gradientRect, 1, GRADIENT_FILL_RECT_H))
  113. fillRect.left = GRADIENT_LEFT;
  114. if (fillRect.left < fillRect.right)
  115. {
  116. COLORREF rgbBackOrig = SetBkColor(contextDst, rgbAlert);
  117. ExtTextOut(contextDst, 0, 0, ETO_OPAQUE, &fillRect, NULL, 0, NULL);
  118. if (rgbBackOrig != rgbAlert)
  119. SetBkColor(contextDst, rgbBackOrig);
  120. }
  121. if (NULL != bitmapIcons)
  122. {
  123. HDC contextSrc = CreateCompatibleDC(hdc);
  124. if (NULL != contextSrc)
  125. {
  126. HBITMAP bitmapSrcOrig = (HBITMAP)SelectObject(contextSrc, bitmapIcons);
  127. BLENDFUNCTION blendFunc;
  128. blendFunc.AlphaFormat = AC_SRC_ALPHA;
  129. blendFunc.BlendFlags = 0;
  130. blendFunc.BlendOp = AC_SRC_OVER;
  131. blendFunc.SourceConstantAlpha = 255;
  132. INT iconTop = (height - iconHeight)/2 + (height - iconHeight)%2;
  133. if (iconTop >= 1 && (iconTop + iconHeight) < height)
  134. {
  135. GdiAlphaBlend(contextDst, GRADIENT_LEFT, iconTop, iconWidth, iconHeight,
  136. contextSrc, iconRect.left, iconRect.top, iconWidth, iconHeight, blendFunc);
  137. }
  138. SelectObject(contextSrc, bitmapSrcOrig);
  139. DeleteDC(contextSrc);
  140. }
  141. }
  142. SelectObject(contextDst, bitmapDstOrig);
  143. }
  144. DeleteDC(contextDst);
  145. }
  146. return bitmapDst;
  147. }
  148. static INT LoginNotifier_CalcTextHeight(HWND hwnd, HDC hdc, INT *aveCharWidth)
  149. {
  150. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  151. if (NULL == notifier) return 0;
  152. HDC contextMine(NULL);
  153. HFONT fontOrig;
  154. if (NULL == hdc)
  155. {
  156. contextMine = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  157. if (NULL == contextMine) return 0;
  158. if (NULL != notifier->font)
  159. fontOrig = (HFONT)SelectObject(contextMine, notifier->font);
  160. hdc = contextMine;
  161. }
  162. TEXTMETRIC tm;
  163. if (FALSE == GetTextMetrics(hdc, &tm))
  164. {
  165. tm.tmHeight = 0;
  166. tm.tmAveCharWidth = 0;
  167. }
  168. if (NULL != aveCharWidth)
  169. {
  170. *aveCharWidth = LoginBox_GetAveCharWidth(hdc);
  171. }
  172. if (NULL != contextMine)
  173. {
  174. if (NULL != notifier->font)
  175. SelectObject(contextMine, fontOrig);
  176. ReleaseDC(hwnd, contextMine);
  177. }
  178. return tm.tmHeight;
  179. }
  180. static void LoginNotifier_Paint(HWND hwnd, HDC hdc, const RECT *prcPaint, BOOL fErase)
  181. {
  182. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  183. if (NULL == notifier) return;
  184. RECT rect;
  185. GetClientRect(hwnd, &rect);
  186. if (NULL == notifier->image)
  187. notifier->image = LoginNotifier_CreateTypeImage(hdc, notifier->type, rect.bottom - rect.top);
  188. INT imageWidth = 0;
  189. if (NULL != notifier->image)
  190. {
  191. BITMAP bm;
  192. if (sizeof(BITMAP) == GetObject(notifier->image, sizeof(BITMAP), &bm))
  193. {
  194. HDC contextSrc = CreateCompatibleDC(hdc);
  195. if (NULL != contextSrc)
  196. {
  197. HBITMAP bitmapSrcOrig = (HBITMAP)SelectObject(contextSrc, notifier->image);
  198. if (FALSE != BitBlt(hdc, rect.left, rect.top, bm.bmWidth, bm.bmHeight, contextSrc, 0, 0, SRCCOPY))
  199. imageWidth = bm.bmWidth;
  200. SelectObject(contextSrc, bitmapSrcOrig);
  201. DeleteDC(contextSrc);
  202. }
  203. }
  204. }
  205. SetBkColor(hdc, notifier->rgbBack);
  206. SetTextColor(hdc, notifier->rgbText);
  207. HFONT fontOrig;
  208. if (NULL != notifier->font)
  209. fontOrig = (HFONT)SelectObject(hdc, notifier->font);
  210. LPCWSTR text = notifier->text;
  211. INT cchText = (NULL != text) ? lstrlenW(text) : 0;
  212. if (-1 == notifier->textHeight)
  213. notifier->textHeight = LoginNotifier_CalcTextHeight(hwnd, hdc, &notifier->aveCharWidth);
  214. RECT textRect;
  215. CopyRect(&textRect, &rect);
  216. textRect.left += imageWidth;
  217. INT textOffsetY = (textRect.bottom - textRect.top) - notifier->textHeight;
  218. textOffsetY = textOffsetY/2 + textOffsetY%2;
  219. if (textOffsetY < SPACING_TOP) textOffsetY = SPACING_TOP;
  220. INT textAlignOrig = SetTextAlign(hdc, TA_TOP | TA_LEFT);
  221. ExtTextOut(hdc, textRect.left + notifier->aveCharWidth+2, textRect.top + textOffsetY, ETO_CLIPPED | ETO_OPAQUE, &textRect, text, cchText, NULL);
  222. if (textAlignOrig != (TA_TOP | TA_LEFT))
  223. SetTextAlign(hdc, textAlignOrig);
  224. if (NULL != notifier->font)
  225. SelectObject(hdc, fontOrig);
  226. }
  227. static void LoginNotifier_UpdateLayout(HWND hwnd, BOOL fRedraw)
  228. {
  229. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  230. if (NULL == notifier) return;
  231. RECT rect;
  232. GetClientRect(hwnd, &rect);
  233. if (NULL != notifier->image)
  234. {
  235. BITMAP bm;
  236. if (sizeof(BITMAP) != GetObject(notifier->image, sizeof(BITMAP), &bm) ||
  237. bm.bmHeight != rect.bottom - rect.top)
  238. {
  239. DeleteObject(notifier->image);
  240. notifier->image = NULL;
  241. }
  242. }
  243. InvalidateRect(hwnd, NULL, TRUE);
  244. }
  245. static BOOL LoginNotifier_SetNotification(HWND hwnd, INT type, LPCWSTR pszText, BOOL fInvalidate)
  246. {
  247. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  248. if (NULL == notifier) return FALSE;
  249. if (type != notifier->type)
  250. {
  251. notifier->type = type;
  252. if (NULL != notifier->image)
  253. {
  254. DeleteObject(notifier->image);
  255. notifier->image = NULL;
  256. }
  257. }
  258. LoginBox_FreeString(notifier->text);
  259. BOOL resultCode = TRUE;
  260. if (NULL == pszText)
  261. {
  262. notifier->text = NULL;
  263. }
  264. else
  265. {
  266. if (IS_INTRESOURCE(pszText))
  267. {
  268. INT stringId;
  269. switch((INT_PTR)pszText)
  270. {
  271. case AUTH_SUCCESS: stringId = IDS_ERR_SUCCESS; break;
  272. case AUTH_404: stringId = IDS_ERR_404; break;
  273. case AUTH_TIMEOUT: stringId = IDS_ERR_TIMEOUT; break;
  274. case AUTH_NOHTTP: stringId = IDS_ERR_NOHTTP; break;
  275. case AUTH_NOPARSER: stringId = IDS_ERR_NOPARSER; break;
  276. case AUTH_CONNECTIONRESET: stringId = IDS_ERR_CONNECTIONRESET; break;
  277. case AUTH_ERROR_PARSING_XML: stringId = IDS_ERR_PARSING_XML; break;
  278. case AUTH_NOT_AUTHORIZED: stringId = IDS_ERR_NOT_AUTHORIZED; break;
  279. case AUTH_SECURID: stringId = IDS_ERR_SECURID; break;
  280. case AUTH_ABORT: stringId = IDS_ERR_ABORT; break;
  281. case AUTH_INVALIDCRED: stringId = IDS_ERR_INVALIDCRED; break;
  282. case AUTH_UNCONFIRMED: stringId = IDS_ERR_UNCONFIRMED; break;
  283. case AUTH_UNEXPECTED: stringId = IDS_ERR_UNEXPECTED; break;
  284. case AUTH_INVALIDPASSCODE: stringId = IDS_ERR_PASSCODE_INVALID; break;
  285. case AUTH_USERNAME_EMPTY: stringId = IDS_ERR_USERNAME_EMPTY; break;
  286. case AUTH_USERNAME_TOOSHORT: stringId = IDS_ERR_USERNAME_TOOSHORT; break;
  287. case AUTH_USERNAME_TOOLONG: stringId = IDS_ERR_USERNAME_TOOLONG; break;
  288. case AUTH_USERNAME_BADFORMAT: stringId = IDS_ERR_USERNAME_BADFORMAT; break;
  289. case AUTH_PASSWORD_EMPTY: stringId = IDS_ERR_PASSWORD_EMPTY; break;
  290. case AUTH_PASSWORD_TOOSHORT: stringId = IDS_ERR_PASSWORD_TOOSHORT; break;
  291. case AUTH_PASSWORD_TOOLONG: stringId = IDS_ERR_PASSWORD_TOOLONG; break;
  292. case AUTH_PASSWORD_BADFORMAT: stringId = IDS_ERR_PASSWORD_BADFORMAT; break;
  293. case AUTH_PASSCODE_EMPTY: stringId = IDS_ERR_PASSCODE_EMPTY; break;
  294. case AUTH_PASSCODE_TOOSHORT: stringId = IDS_ERR_PASSCODE_TOOSHORT; break;
  295. case AUTH_PASSCODE_TOOLONG: stringId = IDS_ERR_PASSCODE_TOOLONG; break;
  296. case AUTH_PASSCODE_BADFORMAT: stringId = IDS_ERR_PASSCODE_BADFORMAT; break;
  297. default: stringId = IDS_ERR_UNKNOWN; break;
  298. }
  299. WCHAR szBuffer[2048] = {0};
  300. LPWSTR cursor = szBuffer;
  301. size_t remaining = ARRAYSIZE(szBuffer);
  302. WASABI_API_LNGSTRINGW_BUF(IDS_LOGIN_FAILURE, cursor, remaining);
  303. size_t len = lstrlen(cursor);
  304. cursor += len;
  305. remaining -= len;
  306. if (cursor != szBuffer)
  307. StringCchCopyEx(cursor, remaining, L": ", &cursor, &remaining, 0);
  308. WASABI_API_LNGSTRINGW_BUF(stringId, cursor, remaining);
  309. notifier->text = LoginBox_CopyString(szBuffer);
  310. if (NULL == notifier->text)
  311. resultCode = FALSE;
  312. }
  313. else
  314. {
  315. notifier->text = LoginBox_CopyString(pszText);
  316. if (NULL == notifier->text)
  317. resultCode = FALSE;
  318. }
  319. }
  320. if (FALSE != fInvalidate)
  321. InvalidateRect(hwnd, NULL, TRUE);
  322. return resultCode;
  323. }
  324. static LRESULT LoginNotifier_OnCreate(HWND hwnd, CREATESTRUCT* pcs)
  325. {
  326. LOGINNOTIFIER *notifier = (LOGINNOTIFIER*)calloc(1, sizeof(LOGINNOTIFIER));
  327. if (NULL != notifier)
  328. {
  329. SetLastError(ERROR_SUCCESS);
  330. if (!SetWindowLongPtr(hwnd, 0, (LONGX86)(LONG_PTR)notifier) && ERROR_SUCCESS != GetLastError())
  331. {
  332. free(notifier);
  333. notifier = NULL;
  334. }
  335. }
  336. if (NULL == notifier)
  337. return -1;
  338. notifier->textHeight = -1;
  339. notifier->rgbBack = RGB(247, 247, 247);
  340. notifier->rgbText = RGB(0, 0, 0);
  341. return 0;
  342. }
  343. static void LoginNotifier_OnDestroy(HWND hwnd)
  344. {
  345. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  346. SetWindowLongPtr(hwnd, 0, 0L);
  347. if (NULL == notifier) return;
  348. if (NULL != notifier->image)
  349. DeleteObject(notifier->image);
  350. free(notifier);
  351. }
  352. static void LoginNotifier_OnWindowPosChanged(HWND hwnd, WINDOWPOS *pwp)
  353. {
  354. if (SWP_NOSIZE == ((SWP_NOSIZE | SWP_FRAMECHANGED) & pwp->flags))
  355. return;
  356. LoginNotifier_UpdateLayout(hwnd, 0 == (SWP_NOREDRAW & pwp->flags));
  357. }
  358. static void LoginNotifier_OnPaint(HWND hwnd)
  359. {
  360. PAINTSTRUCT ps;
  361. if (BeginPaint(hwnd, &ps))
  362. {
  363. if (ps.rcPaint.left != ps.rcPaint.right)
  364. LoginNotifier_Paint(hwnd, ps.hdc, &ps.rcPaint, ps.fErase);
  365. EndPaint(hwnd, &ps);
  366. }
  367. }
  368. static void LoginNotifier_OnPrintClient(HWND hwnd, HDC hdc, UINT options)
  369. {
  370. RECT clientRect;
  371. if (GetClientRect(hwnd, &clientRect))
  372. LoginNotifier_Paint(hwnd, hdc, &clientRect, 0 != (PRF_ERASEBKGND & options));
  373. }
  374. static void LoginNotifier_OnSetFont(HWND hwnd, HFONT hFont, BOOL fRedraw)
  375. {
  376. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  377. if (NULL == notifier) return;
  378. notifier->font = hFont;
  379. notifier->textHeight = -1;
  380. if (NULL != fRedraw)
  381. InvalidateRect(hwnd, NULL, FALSE);
  382. }
  383. static LRESULT LoginNotifier_OnGetFont(HWND hwnd)
  384. {
  385. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  386. return (NULL != notifier) ? (LRESULT)notifier->font : NULL;
  387. }
  388. static LRESULT LoginNotifier_OnSetText(HWND hwnd, LPCWSTR pszText)
  389. {
  390. return LoginNotifier_SetNotification(hwnd, NLNTYPE_INFORMATION, pszText, TRUE);
  391. }
  392. static LRESULT LoginNotifier_OnGetText(HWND hwnd, LPWSTR pszBuffer, size_t cchBufferMax)
  393. {
  394. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  395. LPCWSTR pszText = (NULL != notifier) ? notifier->text : NULL;
  396. if (NULL == pszBuffer)
  397. return 0;
  398. size_t remaining;
  399. StringCchCopyEx(pszBuffer, cchBufferMax, pszText, NULL, &remaining, STRSAFE_IGNORE_NULLS);
  400. return (cchBufferMax - remaining);
  401. }
  402. static LRESULT LoginNotifier_OnGetTextLength(HWND hwnd)
  403. {
  404. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  405. return (NULL != notifier && NULL != notifier->text) ? lstrlen(notifier->text) : 0;
  406. }
  407. static LRESULT LoginNotifier_OnNotify(HWND hwnd, INT type, LPCWSTR pszText)
  408. {
  409. return LoginNotifier_SetNotification(hwnd, type, pszText, TRUE);
  410. }
  411. static LRESULT LoginNotifier_OnGetIdealHeight(HWND hwnd)
  412. {
  413. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  414. if (NULL == notifier) return 0;
  415. if (-1 == notifier->textHeight)
  416. notifier->textHeight = LoginNotifier_CalcTextHeight(hwnd, NULL, &notifier->aveCharWidth);
  417. INT iconHeight(0);
  418. LoginGuiObject *loginGui;
  419. if (SUCCEEDED(LoginGuiObject::QueryInstance(&loginGui)))
  420. {
  421. loginGui->GetIconDimensions(NULL, &iconHeight);
  422. loginGui->Release();
  423. }
  424. INT height = (notifier->textHeight > iconHeight) ?
  425. notifier->textHeight : iconHeight;
  426. height += SPACING_TOP + SPACING_BOTTOM;
  427. return height;
  428. }
  429. static LRESULT LoginNotifier_OnGetIdealSize(HWND hwnd, SIZE *sizeOut)
  430. {
  431. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  432. if (NULL == notifier || NULL == sizeOut) return FALSE;
  433. if (-1 == notifier->textHeight)
  434. notifier->textHeight = LoginNotifier_CalcTextHeight(hwnd, NULL, &notifier->aveCharWidth);
  435. INT iconWidth(0), iconHeight(0);
  436. LoginGuiObject *loginGui;
  437. if (SUCCEEDED(LoginGuiObject::QueryInstance(&loginGui)))
  438. {
  439. loginGui->GetIconDimensions(&iconWidth, &iconHeight);
  440. loginGui->Release();
  441. }
  442. sizeOut->cy = (notifier->textHeight > iconHeight) ?
  443. notifier->textHeight :
  444. iconHeight;
  445. sizeOut->cy += SPACING_TOP + SPACING_BOTTOM;
  446. sizeOut->cx = (0 != iconWidth) ?
  447. (GRADIENT_LEFT + GRADIENT_RIGHT + iconWidth) :
  448. 0;
  449. INT cchText = (NULL != notifier->text) ? lstrlen(notifier->text) : 0;
  450. BOOL resultOk = TRUE;
  451. if (0 != cchText)
  452. {
  453. sizeOut->cx += notifier->aveCharWidth+2;
  454. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_WINDOW | DCX_NORESETATTRS);
  455. if (NULL != hdc)
  456. {
  457. HFONT fontOrig = (HFONT)SelectObject(hdc, notifier->font);
  458. SIZE textSize;
  459. if (FALSE != GetTextExtentPoint32W(hdc, notifier->text, cchText, &textSize))
  460. sizeOut->cx += textSize.cx;
  461. else
  462. resultOk = FALSE;
  463. SelectObject(hdc, fontOrig);
  464. ReleaseDC(hwnd, hdc);
  465. }
  466. else
  467. resultOk = FALSE;
  468. }
  469. return resultOk;
  470. }
  471. static void LoginNotifier_OnSetBkColor(HWND hwnd, COLORREF rgbColor)
  472. {
  473. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  474. if (NULL == notifier) return;
  475. notifier->rgbBack = rgbColor;
  476. }
  477. static LRESULT LoginNotifier_OnGetBkColor(HWND hwnd)
  478. {
  479. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  480. if (NULL == notifier) return RGB(255, 0, 255);
  481. return notifier->rgbBack;
  482. }
  483. static void LoginNotifier_OnSetTextColor(HWND hwnd, COLORREF rgbColor)
  484. {
  485. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  486. if (NULL == notifier) return;
  487. notifier->rgbText = rgbColor;
  488. }
  489. static LRESULT LoginNotifier_OnGetTextColor(HWND hwnd)
  490. {
  491. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  492. if (NULL == notifier) return RGB(255, 0, 255);
  493. return notifier->rgbText;
  494. }
  495. static void LoginNotifier_OnPlayBeep(HWND hwnd)
  496. {
  497. LOGINNOTIFIER *notifier = GetNotifier(hwnd);
  498. if (NULL == notifier) return;
  499. UINT beepType;
  500. switch(notifier->type)
  501. {
  502. case NLNTYPE_INFORMATION: beepType = MB_ICONASTERISK; break;
  503. case NLNTYPE_WARNING: beepType = MB_ICONEXCLAMATION; break;
  504. case NLNTYPE_ERROR: beepType = MB_ICONHAND; break;
  505. case NLNTYPE_QUESTION: beepType = MB_ICONQUESTION; break;
  506. default: return;
  507. }
  508. LoginBox_MessageBeep(beepType);
  509. }
  510. static LRESULT WINAPI LoginNotifier_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  511. {
  512. switch(uMsg)
  513. {
  514. case WM_CREATE: return LoginNotifier_OnCreate(hwnd, (CREATESTRUCT*)lParam);
  515. case WM_DESTROY: LoginNotifier_OnDestroy(hwnd); return 0;
  516. case WM_ERASEBKGND: return 0;
  517. case WM_PAINT: LoginNotifier_OnPaint(hwnd); return 0;
  518. case WM_PRINTCLIENT: LoginNotifier_OnPrintClient(hwnd, (HDC)wParam, (UINT)lParam); return 0;
  519. case WM_PRINT: return 0;
  520. case WM_WINDOWPOSCHANGED: LoginNotifier_OnWindowPosChanged(hwnd, (WINDOWPOS*)lParam); return 0;
  521. case WM_SIZE: return 0;
  522. case WM_SETFONT: LoginNotifier_OnSetFont(hwnd, (HFONT)wParam, (BOOL)LOWORD(lParam)); return 0;
  523. case WM_GETFONT: return LoginNotifier_OnGetFont(hwnd);
  524. case WM_SETTEXT: return LoginNotifier_OnSetText(hwnd, (LPCWSTR)lParam);
  525. case WM_GETTEXT: return LoginNotifier_OnGetText(hwnd, (LPWSTR)lParam, (INT)wParam);
  526. case WM_GETTEXTLENGTH: return LoginNotifier_OnGetTextLength(hwnd);
  527. case NLNM_NOTIFY: return LoginNotifier_OnNotify(hwnd, (INT)wParam, (LPCWSTR)lParam);
  528. case NLNM_GETIDEALHEIGHT: return LoginNotifier_OnGetIdealHeight(hwnd);
  529. case NLNM_SETBKCOLOR: LoginNotifier_OnSetBkColor(hwnd, (COLORREF)lParam); return 0;
  530. case NLNM_GETBKCOLOR: return LoginNotifier_OnGetBkColor(hwnd);
  531. case NLNM_SETTEXTCOLOR: LoginNotifier_OnSetTextColor(hwnd, (COLORREF)lParam); return 0;
  532. case NLNM_GETTEXTCOLOR: return LoginNotifier_OnGetTextColor(hwnd);
  533. case NLNM_PLAYBEEP: LoginNotifier_OnPlayBeep(hwnd); return 0;
  534. case NLNM_GETIDEALSIZE: return LoginNotifier_OnGetIdealSize(hwnd, (SIZE*)lParam);
  535. }
  536. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  537. }