JSAPI2_SecurityPrompt.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #include "main.h"
  2. #include "./resource.h"
  3. #include "./api.h"
  4. #include "./language.h"
  5. #include "./jsapi2_svc_apicreator.h"
  6. #include "./commandLink.h"
  7. #include <strsafe.h>
  8. #ifndef LONGX86
  9. #ifdef _WIN64
  10. #define LONGX86 LONG_PTR
  11. #else /*_WIN64*/
  12. #define LONGX86 LONG
  13. #endif /*_WIN64*/
  14. #endif // LONGX86
  15. #ifdef WIN64
  16. #define MSGRESULT(__hwnd, __result) { SetWindowLongPtrW((__hwnd), DWLP_MSGRESULT, ((LONGX86)(LONG_PTR)(__result))); return TRUE; }
  17. #else
  18. #define MSGRESULT(__hwnd, __result) { SetWindowLongPtrW((__hwnd), DWL_MSGRESULT, ((LONGX86)(LONG_PTR)(__result))); return TRUE; }
  19. #endif
  20. #ifdef __cplusplus
  21. #define SENDMSG(__hwnd, __msgId, __wParam, __lParam) ::SendMessageW((__hwnd), (__msgId), (__wParam), (__lParam))
  22. #else
  23. #define SENDMSG(__hwnd, __msgId, __wParam, __lParam) SendMessageW((__hwnd), (__msgId), (__wParam), (__lParam))
  24. #endif // __cplusplus
  25. #define SENDWAIPC(__ipcMsgId, __param) SENDMSG(hMainWindow, WM_WA_IPC, (WPARAM)(__param), (LPARAM)(__ipcMsgId))
  26. #ifndef OIC_WARNING
  27. #define OIC_WARNING 32515
  28. #endif
  29. #define SECDLG_PROP L"SecurityPromptProp"
  30. #ifndef IDC_HELPLINK
  31. #define IDC_HELPLINK 10000
  32. #endif
  33. typedef struct __SECDLGCREATEPARAM
  34. {
  35. HWND hCenter;
  36. LPCWSTR pszCaption;
  37. LPCWSTR pszTitle;
  38. LPCWSTR pszMessage;
  39. UINT flags;
  40. } SECDLGCREATEPARAM;
  41. typedef struct __SECDLG
  42. {
  43. HFONT titleFont;
  44. SIZE minSize;
  45. SIZE maxSize;
  46. } SECDLG;
  47. #define GetDialog(__hwnd) ((SECDLG*)GetPropW((__hwnd), SECDLG_PROP))
  48. static INT_PTR CALLBACK SecurityPrompt_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  49. static HWND SecurityPrompt_GetPlayerWindow()
  50. {
  51. return (NULL != g_dialog_box_parent) ? g_dialog_box_parent : hMainWindow;
  52. }
  53. INT_PTR JSAPI2_SecurityPrompt(HWND hParent, LPCWSTR pszCaption, LPCWSTR pszTitle, LPCWSTR pszMessage, UINT flags)
  54. {
  55. SECDLGCREATEPARAM param;
  56. ZeroMemory(&param, sizeof(SECDLGCREATEPARAM));
  57. param.hCenter = hParent;
  58. param.flags = flags;
  59. param.pszCaption = pszCaption;
  60. param.pszTitle = pszTitle;
  61. param.pszMessage = pszMessage;
  62. if (NULL == hParent)
  63. hParent = SecurityPrompt_GetPlayerWindow();
  64. return LPDialogBoxParamW(IDD_JSAPI2_AUTHORIZATION2, hParent, SecurityPrompt_DialogProc, (LPARAM)&param);
  65. }
  66. static BOOL SecurityPrompt_GetCenterRect(HWND hCenter, RECT *centerRect, BOOL fUseMonitor)
  67. {
  68. if (NULL == centerRect)
  69. return FALSE;
  70. HWND hDesktop = GetDesktopWindow();
  71. HWND hTest = hCenter;
  72. while(NULL != hTest)
  73. {
  74. DWORD windowStyle = GetWindowLongPtrW(hTest, GWL_STYLE);
  75. if (WS_VISIBLE != ((WS_VISIBLE | WS_MINIMIZE) & windowStyle))
  76. {
  77. hTest = NULL;
  78. hCenter = NULL;
  79. }
  80. else
  81. {
  82. hTest = GetParent(hTest);
  83. }
  84. }
  85. if (FALSE != fUseMonitor || NULL == hCenter || !GetWindowRect(hCenter, centerRect))
  86. {
  87. MONITORINFO mi;
  88. mi.cbSize = sizeof(MONITORINFO);
  89. if (NULL == hCenter)
  90. {
  91. hCenter = SecurityPrompt_GetPlayerWindow();
  92. if (NULL != hCenter)
  93. {
  94. DWORD windowStyle = GetWindowLongPtrW(hCenter, GWL_STYLE);
  95. if (WS_VISIBLE != ((WS_VISIBLE | WS_MINIMIZE) & windowStyle))
  96. hCenter = NULL;
  97. }
  98. }
  99. HMONITOR hMonitor = MonitorFromWindow(hCenter,
  100. (NULL != hCenter) ? MONITOR_DEFAULTTONEAREST : MONITOR_DEFAULTTOPRIMARY);
  101. if (NULL != hMonitor && GetMonitorInfo(hMonitor, &mi))
  102. {
  103. CopyRect(centerRect, &mi.rcWork);
  104. }
  105. else
  106. {
  107. if (NULL == hDesktop || !GetWindowRect(hDesktop, centerRect))
  108. return FALSE;
  109. }
  110. }
  111. return TRUE;
  112. }
  113. static void SecurityPrompt_CenterDialog(HWND hwnd, HWND hCenter)
  114. {
  115. if (NULL == hwnd)
  116. return;
  117. RECT centerRect, windowRect;
  118. if (!GetWindowRect(hwnd, &windowRect) ||
  119. !SecurityPrompt_GetCenterRect(hCenter, &centerRect, FALSE))
  120. {
  121. return;
  122. }
  123. windowRect.left = centerRect.left + ((centerRect.right - centerRect.left) - (windowRect.right - windowRect.left))/2;
  124. windowRect.top = centerRect.top + ((centerRect.bottom - centerRect.top) - (windowRect.bottom - windowRect.top))/2;
  125. SetWindowPos(hwnd, NULL, windowRect.left, windowRect.top, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
  126. }
  127. static void SecurityPrompt_CalculateMinMax(HWND hwnd, HWND hCenter)
  128. {
  129. SECDLG *psd = GetDialog(hwnd);
  130. if (NULL == hwnd || NULL == psd) return;
  131. RECT centerRect, windowRect;
  132. if (GetWindowRect(hwnd, &windowRect))
  133. {
  134. psd->minSize.cx = windowRect.right - windowRect.left;
  135. psd->minSize.cy = windowRect.bottom - windowRect.top;
  136. }
  137. if (SecurityPrompt_GetCenterRect(hCenter, &centerRect, TRUE))
  138. {
  139. InflateRect(&centerRect, -16, -16);
  140. psd->maxSize.cx = centerRect.right - centerRect.left;
  141. psd->maxSize.cy = centerRect.bottom - centerRect.top;
  142. if (psd->maxSize.cx > 360)
  143. psd->maxSize.cx = 360;
  144. }
  145. }
  146. static void SecurityPrompt_LoadIcon(HWND hwnd, HINSTANCE hInstance, LPCWSTR pszIcon)
  147. {
  148. HWND hControl = GetDlgItem(hwnd, IDC_MESSAGEICON);
  149. if (NULL == hControl) return;
  150. SendMessageW(hControl, WM_SETREDRAW, FALSE, 0L);
  151. HICON hIcon = (HICON)LoadImageW(hInstance, pszIcon, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE);
  152. HICON hPrevious = (HICON)SendMessageW(hControl, STM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
  153. if (NULL != hPrevious)
  154. DeleteObject(hPrevious);
  155. if (NULL != hIcon)
  156. {
  157. hPrevious = (HICON)SendMessageW(hControl, STM_GETIMAGE, IMAGE_ICON, 0L);
  158. if (hPrevious != hIcon)
  159. DeleteObject(hIcon);
  160. }
  161. SendMessageW(hControl, WM_SETREDRAW, TRUE, 0L);
  162. if (0 != ShowWindow(hControl, (NULL != hIcon) ? SW_SHOWNA : SW_HIDE))
  163. InvalidateRect(hControl, NULL, TRUE);
  164. }
  165. static BOOL SecurityPrompt_GetWindowTextSize(HWND hwnd, SIZE *pSize, BOOL fMultiline, LONG lineWidth)
  166. {
  167. if (NULL == pSize) return FALSE;
  168. INT cchLen = GetWindowTextLengthW(hwnd);
  169. if (0 == cchLen)
  170. {
  171. ZeroMemory(pSize, sizeof(SIZE));
  172. return TRUE;
  173. }
  174. cchLen++;
  175. LPWSTR pszText = NULL;
  176. WCHAR szBuffer[1024] = {0};
  177. if (cchLen > ARRAYSIZE(szBuffer))
  178. {
  179. pszText = (LPWSTR)calloc(cchLen, sizeof(WCHAR));
  180. if (NULL == pszText) return FALSE;
  181. }
  182. else
  183. {
  184. pszText = szBuffer;
  185. cchLen = ARRAYSIZE(szBuffer);
  186. }
  187. BOOL resultOk = FALSE;
  188. cchLen = GetWindowTextW(hwnd, pszText, cchLen);
  189. if (0 != cchLen)
  190. {
  191. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  192. if (NULL != hdc)
  193. {
  194. HFONT hf = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0L);
  195. HFONT originalFont = (HFONT)SelectObject(hdc, hf);
  196. if (FALSE == fMultiline)
  197. {
  198. resultOk = GetTextExtentPoint32W(hdc, pszText, cchLen, pSize);
  199. }
  200. else
  201. {
  202. RECT textRect;
  203. SetRect(&textRect, 0, 0, lineWidth, 0);
  204. INT h = DrawTextW(hdc, pszText, cchLen, &textRect, DT_CALCRECT | DT_NOPREFIX | DT_NOCLIP | DT_WORDBREAK);
  205. resultOk = (0 != h);
  206. if (resultOk)
  207. {
  208. pSize->cx = textRect.right - textRect.left;
  209. pSize->cy = textRect.bottom - textRect.top;
  210. }
  211. }
  212. SelectObject(hdc, originalFont);
  213. ReleaseDC(hwnd, hdc);
  214. }
  215. }
  216. if (pszText != szBuffer)
  217. free(pszText);
  218. return resultOk;
  219. }
  220. static LONG SecurityPrompt_CalculateFooterHeight(HWND hwnd, LONG marginCY)
  221. {
  222. RECT rect;
  223. HWND hControl;
  224. LONG height = 0;
  225. if (NULL != (hControl = GetDlgItem(hwnd, IDC_BUTTON_ALLOW)) &&
  226. 0 != (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  227. GetWindowRect(hControl, &rect))
  228. {
  229. height += (rect.bottom - rect.top);
  230. }
  231. if (0 == height &&
  232. NULL != (hControl = GetDlgItem(hwnd, IDC_BUTTON_DENY)) &&
  233. 0 != (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  234. GetWindowRect(hControl, &rect))
  235. {
  236. height += (rect.bottom - rect.top);
  237. }
  238. INT szCtrl[] = {IDC_APPLYTOALL, IDC_SEPARATOR, IDC_HELPTEXT, };
  239. for (INT i = 0; i < ARRAYSIZE(szCtrl); i++)
  240. {
  241. if (NULL != (hControl = GetDlgItem(hwnd, szCtrl[i])) &&
  242. 0 != (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  243. GetWindowRect(hControl, &rect))
  244. {
  245. LONG h = (rect.bottom - rect.top);
  246. if (h > 0)
  247. {
  248. if (height > 0) height += marginCY;
  249. height += h;
  250. }
  251. }
  252. }
  253. if (0 != height)
  254. height += 2*marginCY;
  255. return height;
  256. }
  257. static void SecurityPrompt_UpdateLayout(HWND hwnd, BOOL fRedraw)
  258. {
  259. SECDLG *psd = GetDialog(hwnd);
  260. if (NULL == hwnd || NULL == psd) return;
  261. HWND hControl;
  262. RECT rect;
  263. LONG marginCX, marginCY;
  264. SIZE clientSize, iconSize, titleSize, messageSize;
  265. ZeroMemory(&clientSize, sizeof(SIZE));
  266. SetRect(&rect, 6, 6, 6, 6);
  267. if (MapDialogRect(hwnd, &rect))
  268. {
  269. marginCX = rect.left;
  270. marginCY = rect.top;
  271. }
  272. else
  273. {
  274. marginCX = 12;
  275. marginCY = 12;
  276. }
  277. if (NULL != (hControl = GetDlgItem(hwnd, IDC_MESSAGEICON)) &&
  278. 0 != (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  279. GetWindowRect(hControl, &rect))
  280. {
  281. MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rect, 2);
  282. iconSize.cx = rect.right - rect.left;
  283. iconSize.cy = rect.bottom - rect.top;
  284. }
  285. else
  286. {
  287. ZeroMemory(&iconSize, sizeof(SIZE));
  288. }
  289. if (NULL == (hControl = GetDlgItem(hwnd, IDC_TITLE)) ||
  290. 0 == (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) ||
  291. FALSE == SecurityPrompt_GetWindowTextSize(hControl, &titleSize, FALSE, 0))
  292. {
  293. ZeroMemory(&titleSize, sizeof(SIZE));
  294. }
  295. if (NULL == (hControl = GetDlgItem(hwnd, IDC_MESSAGE)) ||
  296. 0 == (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) ||
  297. FALSE == SecurityPrompt_GetWindowTextSize(hControl, &messageSize, TRUE, psd->maxSize.cx - (iconSize.cx + marginCX * 3)))
  298. {
  299. ZeroMemory(&messageSize, sizeof(SIZE));
  300. }
  301. clientSize.cx = iconSize.cx;
  302. if (messageSize.cx > 0)
  303. {
  304. clientSize.cx += messageSize.cx;
  305. if (iconSize.cx > 0) clientSize.cx += marginCX;
  306. }
  307. if (titleSize.cx > clientSize.cx)
  308. titleSize.cx = clientSize.cx;
  309. clientSize.cx += 2*marginCX;
  310. clientSize.cy = titleSize.cy;
  311. LONG h1 = messageSize.cy;
  312. if (h1 > 0) h1 += 2 * marginCY;
  313. LONG h2 = iconSize.cy;
  314. if (h2 > 0) h2 += marginCY;
  315. clientSize.cy += (h1 > h2) ? h1 : h2;
  316. if (clientSize.cy > 0)
  317. {
  318. if (clientSize.cy != titleSize.cy && 0 != titleSize.cy)
  319. clientSize.cy += marginCY;
  320. }
  321. clientSize.cy += SecurityPrompt_CalculateFooterHeight(hwnd, marginCY);
  322. DWORD windowStyle = GetWindowLongPtrW(hwnd, GWL_STYLE);
  323. DWORD windowExStyle = GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
  324. SetRect(&rect, 0, 0, clientSize.cx, clientSize.cy);
  325. if (AdjustWindowRectEx(&rect, windowStyle, FALSE, windowExStyle))
  326. {
  327. clientSize.cx = rect.right - rect.left;
  328. clientSize.cy = rect.bottom - rect.top;
  329. }
  330. if (clientSize.cx < psd->minSize.cx) clientSize.cx = psd->minSize.cx;
  331. if (clientSize.cy < psd->minSize.cy) clientSize.cy = psd->minSize.cy;
  332. SetWindowPos(hwnd, NULL, 0, 0, clientSize.cx, clientSize.cy, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  333. RECT clientRect;
  334. if (!GetClientRect(hwnd, &clientRect))
  335. SetRectEmpty(&clientRect);
  336. else
  337. InflateRect(&clientRect, -marginCX, -marginCY);
  338. if (NULL != (hControl = GetDlgItem(hwnd, IDC_TITLE)))
  339. {
  340. SetWindowPos(hControl, NULL, clientRect.left, clientRect.top, clientRect.right - clientRect.left, titleSize.cy, SWP_NOZORDER | SWP_NOACTIVATE);
  341. }
  342. if (NULL != (hControl = GetDlgItem(hwnd, IDC_MESSAGEICON)))
  343. {
  344. LONG y = clientRect.top;
  345. if (titleSize.cy > 0) y += (titleSize.cy + marginCY);
  346. SetWindowPos(hControl, NULL, clientRect.left, y, iconSize.cx, iconSize.cy, SWP_NOZORDER | SWP_NOACTIVATE);
  347. }
  348. if (NULL != (hControl = GetDlgItem(hwnd, IDC_MESSAGE)))
  349. {
  350. LONG x = clientRect.left;
  351. if (iconSize.cx > 0) x += (iconSize.cx + marginCX);
  352. LONG y = clientRect.top;
  353. if (titleSize.cy > 0) y += (titleSize.cy + marginCY);
  354. SetWindowPos(hControl, NULL, x, y, clientRect.right - x, messageSize.cy, SWP_NOZORDER | SWP_NOACTIVATE);
  355. }
  356. HWND hHelp;
  357. hHelp = GetDlgItem(hwnd, IDC_HELPTEXT);
  358. if (NULL == hHelp || !GetWindowRect(hHelp, &rect))
  359. SetRectEmpty(&rect);
  360. else
  361. MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rect, 2);
  362. SIZE linkSize;
  363. LONG bottomLine = clientRect.bottom;
  364. if (NULL != (hControl = GetDlgItem(hwnd, IDC_HELPLINK)) &&
  365. CommandLink_GetIdealSize(hControl, &linkSize))
  366. {
  367. RECT margins;
  368. if (!CommandLink_GetMargins(hControl, &margins))
  369. SetRectEmpty(&margins);
  370. if (linkSize.cy > 0)
  371. {
  372. bottomLine -= (linkSize.cy - margins.bottom);
  373. }
  374. SetWindowPos(hControl, NULL, clientRect.left - margins.left, bottomLine, linkSize.cx, linkSize.cy, SWP_NOACTIVATE | SWP_NOZORDER);
  375. if (NULL != hHelp)
  376. {
  377. LONG x = clientRect.left + linkSize.cx - margins.right;
  378. LONG cy = rect.bottom - rect.top;
  379. SetWindowPos(hHelp, NULL, x, clientRect.bottom - cy, clientRect.right - x, cy, SWP_NOZORDER | SWP_NOACTIVATE);
  380. }
  381. }
  382. if (NULL != (hControl = GetDlgItem(hwnd, IDC_SEPARATOR)) &&
  383. 0 != (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  384. GetWindowRect(hControl, &rect))
  385. {
  386. bottomLine -= (marginCY + (rect.bottom - rect.top));
  387. SetWindowPos(hControl, NULL, 0, bottomLine, clientRect.right - clientRect.left + 2*marginCX + 2, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
  388. }
  389. if (NULL != (hControl = GetDlgItem(hwnd, IDC_APPLYTOALL)) &&
  390. 0 != (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  391. GetWindowRect(hControl, &rect))
  392. {
  393. bottomLine -= (marginCY + (rect.bottom - rect.top));
  394. SetWindowPos(hControl, NULL, clientRect.left, bottomLine, clientRect.right - clientRect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
  395. }
  396. LONG buttonRight = clientRect.right;
  397. if (NULL != (hControl = GetDlgItem(hwnd, IDC_BUTTON_DENY)) &&
  398. 0 != (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  399. GetWindowRect(hControl, &rect))
  400. {
  401. SetWindowPos(hControl, NULL, buttonRight - (rect.right - rect.left), bottomLine - (marginCY + (rect.bottom - rect.top)),
  402. rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
  403. if (rect.right > rect.left)
  404. {
  405. buttonRight -= ((rect.right - rect.left) + marginCX);
  406. }
  407. }
  408. if (NULL != (hControl = GetDlgItem(hwnd, IDC_BUTTON_ALLOW)) &&
  409. 0 != (WS_VISIBLE & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  410. GetWindowRect(hControl, &rect))
  411. {
  412. SetWindowPos(hControl, NULL, buttonRight - (rect.right - rect.left), bottomLine - (marginCY + (rect.bottom - rect.top)),
  413. rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
  414. }
  415. }
  416. static HFONT SecurityPrompt_GetTitleFont(HWND hwnd)
  417. {
  418. SECDLG *psd = GetDialog(hwnd);
  419. if (NULL == psd)
  420. return (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0L);
  421. if (NULL != psd->titleFont)
  422. return psd->titleFont;
  423. LOGFONTW lf;
  424. ZeroMemory(&lf, sizeof(LOGFONTW));
  425. HFONT hf = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0L);
  426. if (NULL != hf)
  427. GetObjectW(hf, sizeof(LOGFONTW), &lf);
  428. if (L'\0' == lf.lfFaceName[0])
  429. {
  430. if (!SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(LOGFONTW), &lf, 0) ||
  431. FAILED(StringCchCopyW(lf.lfFaceName, ARRAYSIZE(lf.lfFaceName), L"MS Shell Dlg 2")))
  432. {
  433. lf.lfFaceName[0] = L'\0';
  434. }
  435. }
  436. if (L'\0' != lf.lfFaceName[0])
  437. {
  438. lf.lfWeight = FW_SEMIBOLD;
  439. psd->titleFont = CreateFontIndirectW(&lf);
  440. }
  441. return psd->titleFont;
  442. }
  443. static BOOL SecurityPrompt_ShowHelp(HWND hwnd)
  444. {
  445. INT result = (INT)(INT_PTR)ShellExecuteW(hwnd, L"open",
  446. L"https://help.winamp.com/hc/articles/8112753225364-Online-Services-Security",
  447. NULL, NULL, SW_SHOWNORMAL);
  448. return (result > 32);
  449. }
  450. static INT_PTR SecurityPrompt_OnInitDialog(HWND hwnd, HWND hFocus, LPARAM param)
  451. {
  452. SECDLGCREATEPARAM *createParam = (SECDLGCREATEPARAM*)param;
  453. if (NULL == createParam) return 0;
  454. SECDLG *psd = (SECDLG*)calloc(1, sizeof(SECDLG));
  455. if (NULL != psd)
  456. {
  457. if (!SetPropW(hwnd, SECDLG_PROP, psd))
  458. {
  459. free(psd);
  460. psd = NULL;
  461. }
  462. }
  463. HMENU hMenu = GetSystemMenu(hwnd, FALSE);
  464. if (NULL != hMenu)
  465. {
  466. EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND|MF_DISABLED);
  467. DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
  468. DeleteMenu(hMenu, SC_SIZE, MF_BYCOMMAND);
  469. }
  470. SecurityPrompt_LoadIcon(hwnd, NULL, MAKEINTRESOURCEW(OIC_WARNING));
  471. WCHAR szBuffer[4096] = {0};
  472. LPCWSTR pszText;
  473. HWND hControl;
  474. SecurityPrompt_CalculateMinMax(hwnd, createParam->hCenter);
  475. if (NULL != (pszText = createParam->pszCaption) && IS_INTRESOURCE(pszText))
  476. pszText = getStringW((INT)(INT_PTR)pszText, szBuffer, ARRAYSIZE(szBuffer));
  477. if (NULL != pszText && L'\0' != *pszText)
  478. SetWindowTextW(hwnd, pszText);
  479. if (NULL != (hControl = GetDlgItem(hwnd, IDC_TITLE)))
  480. {
  481. if (NULL != (pszText = createParam->pszTitle) && IS_INTRESOURCE(pszText))
  482. pszText = getStringW((INT)(INT_PTR)pszText, szBuffer, ARRAYSIZE(szBuffer));
  483. HFONT hf = SecurityPrompt_GetTitleFont(hwnd);
  484. if (NULL != hf) SendMessageW(hControl, WM_SETFONT, (WPARAM)hf, 0L);
  485. SetWindowTextW(hControl, pszText);
  486. ShowWindow(hControl, (NULL != pszText && L'\0' != *pszText) ? SW_SHOWNA : SW_HIDE);
  487. }
  488. if (NULL != (hControl = GetDlgItem(hwnd, IDC_MESSAGE)))
  489. {
  490. if (NULL != (pszText = createParam->pszMessage) && IS_INTRESOURCE(pszText))
  491. pszText = getStringW((INT)(INT_PTR)pszText, szBuffer, ARRAYSIZE(szBuffer));
  492. SetWindowTextW(hControl, pszText);
  493. ShowWindow(hControl, (NULL != pszText && L'\0' != *pszText) ? SW_SHOWNA : SW_HIDE);
  494. }
  495. getStringW(IDS_CLICKHERE, szBuffer, ARRAYSIZE(szBuffer));
  496. HWND hLink = CreateWindowExW(WS_EX_NOPARENTNOTIFY, NWC_COMMANDLINKW, szBuffer,
  497. WS_VISIBLE | WS_CHILD | WS_TABSTOP | /*CLS_HOTTRACK | */CLS_DEFAULTCOLORS | CLS_ALWAYSUNDERLINE,
  498. 0, 0, 0, 0, hwnd, (HMENU)IDC_HELPLINK, hMainInstance, NULL);
  499. if (NULL != hLink)
  500. {
  501. if (NULL != (hControl = GetDlgItem(hwnd, IDC_APPLYTOALL)))
  502. SetWindowPos(hLink, hControl, 0, 0, 0, 0, SWP_NOACTIVATE |SWP_NOSIZE | SWP_NOMOVE);
  503. SendMessageW(hLink, WM_SETFONT, (WPARAM)SendMessageW(hwnd, WM_GETFONT, 0, 0L), 0L);
  504. }
  505. CheckDlgButton(hwnd, IDC_APPLYTOALL,
  506. (0 != (JSAPI2::svc_apicreator::AUTHORIZATION_FLAG_ALWAYS_FOR_SERVICE &createParam->flags)) ?
  507. BST_CHECKED : BST_UNCHECKED);
  508. SecurityPrompt_UpdateLayout(hwnd, FALSE);
  509. SecurityPrompt_CenterDialog(hwnd, createParam->hCenter);
  510. SendMessageW(hwnd, DM_REPOSITION, 0, 0L);
  511. DWORD dialogThread = GetWindowThreadProcessId(hwnd, NULL);
  512. DWORD parentThread = GetWindowThreadProcessId(GetParent(hwnd), NULL);
  513. if (dialogThread != parentThread)
  514. {
  515. AttachThreadInput(parentThread, dialogThread, TRUE);
  516. SetForegroundWindow(hwnd);
  517. }
  518. if (NULL != (hControl = GetDlgItem(hwnd, IDC_BUTTON_DENY)) &&
  519. WS_VISIBLE == ((WS_VISIBLE | WS_DISABLED) & GetWindowLongPtrW(hControl, GWL_STYLE)) &&
  520. PostMessageW(hwnd, WM_NEXTDLGCTL, (WPARAM)hControl, TRUE))
  521. {
  522. return TRUE;
  523. }
  524. return FALSE;
  525. }
  526. static void SecurityPrompt_OnDestroy(HWND hwnd)
  527. {
  528. SECDLG *psd = GetDialog(hwnd);
  529. RemovePropW(hwnd, SECDLG_PROP);
  530. DWORD dialogThread = GetWindowThreadProcessId(hwnd, NULL);
  531. DWORD parentThread = GetWindowThreadProcessId(GetParent(hwnd), NULL);
  532. if (dialogThread != parentThread)
  533. {
  534. AttachThreadInput(parentThread, dialogThread, FALSE);
  535. }
  536. if (NULL == psd) return;
  537. if (NULL != psd->titleFont)
  538. DeleteObject(psd->titleFont);
  539. free(psd);
  540. }
  541. static void SecurityPrompt_OnCommand(HWND hwnd, INT commandId, INT eventId, HWND hControl)
  542. {
  543. switch(commandId)
  544. {
  545. case IDC_BUTTON_ALLOW:
  546. case IDC_BUTTON_DENY:
  547. if(BN_CLICKED == eventId)
  548. {
  549. INT_PTR result = (IDC_BUTTON_ALLOW == commandId) ?
  550. JSAPI2::svc_apicreator::AUTHORIZATION_ALLOW :
  551. JSAPI2::svc_apicreator::AUTHORIZATION_DENY;
  552. result |= (BST_CHECKED == IsDlgButtonChecked(hwnd, IDC_APPLYTOALL)) ?
  553. JSAPI2::svc_apicreator::AUTHORIZATION_FLAG_ALWAYS_FOR_SERVICE :
  554. JSAPI2::svc_apicreator::AUTHORIZATION_FLAG_ALWAYS;
  555. EndDialog(hwnd, result);
  556. }
  557. break;
  558. }
  559. }
  560. static LRESULT SecurityPrompt_OnNotify(HWND hwnd, INT controlId, NMHDR *pnmh)
  561. {
  562. switch(controlId)
  563. {
  564. case IDC_HELPLINK:
  565. if (NM_CLICK == pnmh->code)
  566. SecurityPrompt_ShowHelp(hwnd);
  567. return TRUE;
  568. }
  569. return 0;
  570. }
  571. static INT_PTR CALLBACK SecurityPrompt_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  572. {
  573. switch(uMsg)
  574. {
  575. case WM_INITDIALOG: return SecurityPrompt_OnInitDialog(hwnd, (HWND)wParam, lParam);
  576. case WM_DESTROY: SecurityPrompt_OnDestroy(hwnd); break;
  577. case WM_COMMAND: SecurityPrompt_OnCommand(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break;
  578. case WM_NOTIFY: MSGRESULT(hwnd, SecurityPrompt_OnNotify(hwnd, (INT)wParam, (NMHDR*)lParam));
  579. }
  580. return 0;
  581. }