1
0

curtain.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. #include "main.h"
  2. #include "./curtain.h"
  3. #include "./graphics.h"
  4. #include "./resource.h"
  5. #include "../winamp/wa_dlg.h"
  6. #include "../Plugins/General/gen_ml/ml_ipc_0313.h"
  7. #include "./ifc_imageloader.h"
  8. #include "./ifc_skinhelper.h"
  9. #include <strsafe.h>
  10. #include <windows.h>
  11. #define WIDGET_MINWIDTH_UNITS 60
  12. #define WIDGET_MAXWIDTH_UNITS 200
  13. #define WIDGET_FRAMECX 1
  14. #define WIDGET_FRAMECY 1
  15. #define WIDGET_SPACECX_UNITS 8
  16. #define WIDGET_SPACECY_UNITS 8
  17. #define WIDGET_CONTROLSPACE_UNITS 6
  18. #define PROGRESS_FRAMECOUNT 24
  19. #define ANIMATETIMER_ID 64
  20. #define ANIMATETIMER_INTERVAL 1000 / PROGRESS_FRAMECOUNT
  21. typedef struct __CURTAIN
  22. {
  23. LPWSTR pszTitle;
  24. LPWSTR pszOperation;
  25. HFONT textFont;
  26. RECT widgetRect;
  27. HBRUSH backBrush;
  28. HBRUSH widgetBrush;
  29. HBRUSH frameBrush;
  30. HBITMAP progressBitmap;
  31. INT frameNumber;
  32. SIZE titleSize;
  33. SIZE operationSize;
  34. SIZE imageSize;
  35. COLORREF rgbBk;
  36. COLORREF rgbFg;
  37. } CURTAIN;
  38. #define GetCurtain(__hwnd) ((CURTAIN*)(LONG_PTR)(LONGX86)GetWindowLongPtr((__hwnd), 0))
  39. static LRESULT CALLBACK Curtain_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  40. BOOL Curtain_RegisterClass(HINSTANCE hInstance)
  41. {
  42. WNDCLASS wc = {0};
  43. if (GetClassInfo(hInstance, NWC_ONLINEMEDIACURTAIN, &wc)) return TRUE;
  44. ZeroMemory(&wc, sizeof(WNDCLASS));
  45. wc.hInstance = hInstance;
  46. wc.lpszClassName = NWC_ONLINEMEDIACURTAIN;
  47. wc.lpfnWndProc = Curtain_WindowProc;
  48. wc.style = CS_DBLCLKS;
  49. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  50. wc.hbrBackground = NULL;
  51. wc.cbWndExtra = sizeof(CURTAIN*);
  52. return ( 0 != RegisterClassW(&wc));
  53. }
  54. #define RA_LEFT 0x0001
  55. #define RA_RIGHT 0x0002
  56. #define RA_HCENTER 0x0003
  57. #define RA_TOP 0x0010
  58. #define RA_BOTTOM 0x0020
  59. #define RA_VCENTER 0x0030
  60. #define RA_FITHORZ 0x0100
  61. #define RA_FITVERT 0x0200
  62. static COLORREF Curtain_GetColor(INT skinColor, INT sysColor)
  63. {
  64. COLORREF rgb;
  65. ifc_skinhelper *skinHelper = NULL;
  66. HRESULT hr = Plugin_GetSkinHelper(&skinHelper);
  67. if (SUCCEEDED(hr) && skinHelper != NULL)
  68. {
  69. hr = skinHelper->GetColor(skinColor, &rgb);
  70. skinHelper->Release();
  71. }
  72. if (FAILED(hr))
  73. rgb = GetSysColor(sysColor);
  74. return rgb;
  75. }
  76. static void Curtain_RectAlign(RECT *prcTarget, const RECT *prcBounds, UINT flags)
  77. {
  78. if (0 != (0x0F & flags)) // horz
  79. {
  80. LONG targetWidth = prcTarget->right - prcTarget->left;
  81. LONG boundsWidth = prcBounds->right - prcBounds->left;
  82. if (0 != (RA_FITHORZ & flags) && targetWidth > boundsWidth)
  83. targetWidth = boundsWidth;
  84. if (targetWidth == boundsWidth)
  85. {
  86. prcTarget->left = prcBounds->left;
  87. prcTarget->right = prcBounds->right;
  88. }
  89. else
  90. {
  91. switch(0x0F & flags)
  92. {
  93. case RA_HCENTER: prcTarget->left = prcBounds->left + (boundsWidth - targetWidth)/2; break;
  94. case RA_LEFT: prcTarget->left = prcBounds->left; break;
  95. case RA_RIGHT: prcTarget->left = prcBounds->right - targetWidth; break;
  96. }
  97. prcTarget->right = prcTarget->left + targetWidth;
  98. }
  99. }
  100. if (0 != (0xF0 & flags)) // horz
  101. {
  102. LONG targetHeight = prcTarget->bottom - prcTarget->top;
  103. LONG boundsHeight = prcBounds->bottom - prcBounds->top;
  104. if (0 != (RA_FITVERT & flags) && targetHeight > boundsHeight)
  105. targetHeight = boundsHeight;
  106. if (targetHeight == boundsHeight)
  107. {
  108. prcTarget->top = prcBounds->top;
  109. prcTarget->bottom = prcBounds->bottom;
  110. }
  111. else
  112. {
  113. switch(0xF0 & flags)
  114. {
  115. case RA_VCENTER: prcTarget->top = prcBounds->top + (boundsHeight - targetHeight)/2; break;
  116. case RA_TOP: prcTarget->top = prcBounds->top; break;
  117. case RA_BOTTOM: prcTarget->top = prcBounds->bottom - targetHeight; break;
  118. }
  119. prcTarget->bottom = prcTarget->top + targetHeight;
  120. }
  121. }
  122. }
  123. static BOOL Curtain_SetPluginString(LPWSTR *ppDest, LPCWSTR pszSource)
  124. {
  125. if (NULL != *ppDest)
  126. Plugin_FreeString(*ppDest);
  127. if(NULL == pszSource)
  128. {
  129. *ppDest = NULL;
  130. }
  131. else
  132. {
  133. WCHAR szBuffer[1024] = {0};
  134. if (IS_INTRESOURCE(pszSource))
  135. {
  136. Plugin_LoadString((INT)(INT_PTR)pszSource, szBuffer, ARRAYSIZE(szBuffer));
  137. pszSource = szBuffer;
  138. }
  139. *ppDest = Plugin_CopyString(pszSource);
  140. }
  141. return TRUE;
  142. }
  143. static BOOL Curtain_GetTextSize(LPCWSTR pszText, HWND hwnd, SIZE *textSize)
  144. {
  145. if (NULL == hwnd || NULL == textSize)
  146. return FALSE;
  147. HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0L);
  148. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE);
  149. if (NULL == hdc) return FALSE;
  150. HFONT originalFont = (HFONT)SelectObject(hdc, font);
  151. INT cchText = (NULL != pszText) ? lstrlen(pszText) : 0;
  152. BOOL result = FALSE;
  153. if ( 0 == cchText)
  154. {
  155. TEXTMETRIC tm = {0};
  156. if (GetTextMetrics(hdc, &tm))
  157. {
  158. textSize->cy = tm.tmHeight;
  159. textSize->cx = 0;
  160. result = TRUE;
  161. }
  162. }
  163. else
  164. {
  165. if (GetTextExtentPoint32(hdc, pszText, cchText, textSize))
  166. result = TRUE;
  167. }
  168. SelectObject(hdc, originalFont);
  169. ReleaseDC(hwnd, hdc);
  170. return result;
  171. }
  172. static BOOL Curtain_MapDialogRectHdc(HDC hdc, RECT *prc)
  173. {
  174. TEXTMETRIC tm;
  175. if (NULL == prc || !GetTextMetrics(hdc, &tm))
  176. return FALSE;
  177. prc->left = MulDiv(prc->left, tm.tmAveCharWidth, 4);
  178. prc->right = MulDiv(prc->right, tm.tmAveCharWidth, 4);
  179. prc->top = MulDiv(prc->top, tm.tmHeight, 8);
  180. prc->bottom = MulDiv(prc->bottom, tm.tmHeight, 8);
  181. return TRUE;
  182. }
  183. static BOOL Curtain_MapDialogRect(HWND hwnd, RECT *prc)
  184. {
  185. HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0L);
  186. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE);
  187. if (NULL == hdc) return FALSE;
  188. HFONT originalFont = (HFONT)SelectObject(hdc, font);
  189. BOOL result = Curtain_MapDialogRectHdc(hdc, prc);
  190. SelectObject(hdc, originalFont);
  191. ReleaseDC(hwnd, hdc);
  192. return result;
  193. }
  194. static BOOL Curtain_GetImageSize(HBITMAP bitmap, INT frameCount, SIZE *imageSize)
  195. {
  196. if (NULL == bitmap || 0 == frameCount || NULL == imageSize)
  197. return FALSE;
  198. BITMAP bm;
  199. if (sizeof(BITMAP) != GetObject(bitmap, sizeof(BITMAP), &bm))
  200. return FALSE;
  201. imageSize->cx = bm.bmWidth;
  202. imageSize->cy = bm.bmHeight / frameCount;
  203. return TRUE;
  204. }
  205. HBITMAP Curtain_LoadImage(HWND hwnd, INT frameCount, COLORREF rgbBk, COLORREF rgbFg)
  206. {
  207. HBITMAP frameBitmap = NULL;
  208. BITMAPINFOHEADER header = {0};
  209. BYTE *pixelData = NULL;
  210. ifc_omimageloader *loader = NULL;
  211. if (SUCCEEDED(Plugin_QueryImageLoader(Plugin_GetInstance(), MAKEINTRESOURCE(IDR_CURTAINPROGRESS_IMAGE), FALSE, &loader)) && loader != NULL)
  212. {
  213. loader->LoadBitmapEx(&frameBitmap, &header, (void**)&pixelData);
  214. loader->Release();
  215. }
  216. if (NULL == frameBitmap)
  217. return NULL;
  218. if (header.biHeight < 0) header.biHeight = - header.biHeight;
  219. Image_Colorize(pixelData, header.biWidth, header.biHeight, header.biBitCount, rgbBk, rgbFg, TRUE);
  220. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE);
  221. HBITMAP bitmap = Image_AnimateRotation(hdc, frameBitmap, frameCount, rgbBk, FALSE);
  222. ReleaseDC(hwnd, hdc);
  223. DeleteObject(frameBitmap);
  224. return bitmap;
  225. }
  226. static void Curtain_UpdateLayout(HWND hwnd, BOOL fRedraw)
  227. {
  228. CURTAIN *curtain = GetCurtain(hwnd);
  229. if (NULL == curtain) return;
  230. RECT clientRect;
  231. if (!GetClientRect(hwnd, &clientRect))
  232. return;
  233. InflateRect(&clientRect, -8, -8);
  234. if (!Curtain_GetImageSize(curtain->progressBitmap, PROGRESS_FRAMECOUNT, &curtain->imageSize))
  235. ZeroMemory(&curtain->imageSize, sizeof(SIZE));
  236. if (!Curtain_GetTextSize(curtain->pszTitle, hwnd, &curtain->titleSize))
  237. ZeroMemory(&curtain->titleSize, sizeof(SIZE));
  238. if (!Curtain_GetTextSize(curtain->pszOperation, hwnd, &curtain->operationSize))
  239. ZeroMemory(&curtain->operationSize, sizeof(SIZE));
  240. RECT spaceRect;
  241. SetRect(&spaceRect, WIDGET_SPACECX_UNITS, WIDGET_SPACECY_UNITS, 0, WIDGET_CONTROLSPACE_UNITS);
  242. Curtain_MapDialogRect(hwnd, &spaceRect);
  243. INT spaceCY = spaceRect.top;
  244. INT intervalCY = spaceRect.bottom;
  245. INT widgetHeight = 2 * spaceCY + curtain->titleSize.cy;
  246. if (widgetHeight > (clientRect.bottom - clientRect.top))
  247. {
  248. widgetHeight = 0;
  249. ZeroMemory(&curtain->titleSize, sizeof(SIZE));
  250. }
  251. if (0 != widgetHeight && curtain->operationSize.cy > 0 &&
  252. (widgetHeight + curtain->operationSize.cy + intervalCY) < (clientRect.bottom - clientRect.top))
  253. widgetHeight += curtain->operationSize.cy + intervalCY;
  254. else
  255. ZeroMemory(&curtain->operationSize, sizeof(SIZE));
  256. if (0 != widgetHeight && curtain->imageSize.cy > 0 &&
  257. (widgetHeight + curtain->imageSize.cy + intervalCY) < (clientRect.bottom - clientRect.top))
  258. widgetHeight += curtain->imageSize.cy + intervalCY;
  259. else
  260. ZeroMemory(&curtain->imageSize, sizeof(SIZE));
  261. RECT limitRect, prevRect;
  262. CopyRect(&prevRect, &curtain->widgetRect);
  263. SetRect(&limitRect, WIDGET_MINWIDTH_UNITS, 0, WIDGET_MAXWIDTH_UNITS, 0);
  264. Curtain_MapDialogRect(hwnd, &limitRect);
  265. if ( 0 != widgetHeight && (clientRect.right - clientRect.left) >= limitRect.left)
  266. {
  267. curtain->widgetRect.left = clientRect.left;
  268. curtain->widgetRect.top = clientRect.top;
  269. curtain->widgetRect.right = ((clientRect.right - clientRect.left) >= limitRect.right) ?
  270. (curtain->widgetRect.left + limitRect.right) :
  271. (clientRect.right - clientRect.left);
  272. curtain->widgetRect.bottom = clientRect.top + widgetHeight;
  273. Curtain_RectAlign(&curtain->widgetRect, &clientRect, RA_HCENTER | RA_VCENTER);
  274. }
  275. else
  276. {
  277. SetRectEmpty(&curtain->widgetRect);
  278. ZeroMemory(&curtain->titleSize, sizeof(SIZE));
  279. ZeroMemory(&curtain->operationSize, sizeof(SIZE));
  280. ZeroMemory(&curtain->imageSize, sizeof(SIZE));
  281. }
  282. if (FALSE != fRedraw && FALSE == EqualRect(&curtain->widgetRect, &prevRect))
  283. {
  284. InvalidateRect(hwnd, &prevRect, TRUE);
  285. InvalidateRect(hwnd, &curtain->widgetRect, FALSE);
  286. }
  287. }
  288. static HRGN Curtain_GetFrameRgn(RECT *prcWidget)
  289. {
  290. HRGN regionFrame, regionPart;
  291. regionFrame = CreateRectRgn(prcWidget->left, prcWidget->top, prcWidget->right, prcWidget->top + WIDGET_FRAMECY);
  292. regionPart = CreateRectRgn(prcWidget->left, prcWidget->bottom - WIDGET_FRAMECY, prcWidget->right, prcWidget->bottom);
  293. CombineRgn(regionFrame, regionFrame, regionPart, RGN_OR);
  294. SetRectRgn(regionPart, prcWidget->left, prcWidget->top + WIDGET_FRAMECY, prcWidget->left + WIDGET_FRAMECX, prcWidget->bottom - WIDGET_FRAMECY);
  295. CombineRgn(regionFrame, regionFrame, regionPart, RGN_OR);
  296. SetRectRgn(regionPart, prcWidget->right - WIDGET_FRAMECX, prcWidget->top + WIDGET_FRAMECY, prcWidget->right, prcWidget->bottom - WIDGET_FRAMECY);
  297. CombineRgn(regionFrame, regionFrame, regionPart, RGN_OR);
  298. DeleteObject(regionPart);
  299. return regionFrame;
  300. }
  301. static void Curtain_PaintWidgetBack(CURTAIN *curtain, HDC hdc, HRGN regionPaint)
  302. {
  303. HRGN regionFrame = Curtain_GetFrameRgn(&curtain->widgetRect);
  304. if (NULL == curtain->frameBrush)
  305. curtain->frameBrush = CreateSolidBrush(Curtain_GetColor(WADLG_HILITE, COLOR_3DHILIGHT));
  306. if (FillRgn(hdc, regionFrame, curtain->frameBrush))
  307. CombineRgn(regionPaint, regionPaint, regionFrame, RGN_DIFF);
  308. DeleteObject(regionFrame);
  309. if (NULL == curtain->widgetBrush)
  310. curtain->widgetBrush = CreateSolidBrush(Curtain_GetColor(WADLG_WNDBG, COLOR_WINDOW));
  311. FillRgn(hdc, regionPaint, curtain->widgetBrush);
  312. }
  313. static BOOL Curtain_PaintImage(HBITMAP bitmap, INT frame, const RECT *bitmapRect, HDC hdc, const RECT *paintRect)
  314. {
  315. RECT blitRect;
  316. if (!IntersectRect(&blitRect, bitmapRect, paintRect))
  317. return TRUE;
  318. BOOL success = FALSE;
  319. HDC hdcSrc = CreateCompatibleDC(hdc);
  320. if (NULL != hdcSrc)
  321. {
  322. HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcSrc, bitmap);
  323. success = BitBlt(hdc, blitRect.left, blitRect.top,
  324. blitRect.right - blitRect.left, blitRect.bottom - blitRect.top,
  325. hdcSrc,
  326. blitRect.left - bitmapRect->left,
  327. (bitmapRect->bottom - bitmapRect->top)*frame + blitRect.top - bitmapRect->top,
  328. SRCCOPY);
  329. SelectObject(hdcSrc, hbmpOld);
  330. DeleteDC(hdcSrc);
  331. }
  332. return success;
  333. }
  334. static BOOL Curtain_PaintWidget(CURTAIN *curtain, HDC hdc, RECT *prcPaint, BOOL fErase)
  335. {
  336. HRGN regionPaint = CreateRectRgnIndirect(prcPaint);
  337. HRGN regionPart = CreateRectRgn(0,0,0,0);
  338. COLORREF originalBk = SetBkColor(hdc, curtain->rgbBk);
  339. COLORREF originalFg = SetTextColor(hdc, curtain->rgbFg);
  340. RECT clientRect;
  341. CopyRect(&clientRect, &curtain->widgetRect);
  342. HFONT originalFont = (HFONT)SelectObject(hdc, (NULL != curtain->textFont) ?
  343. curtain->textFont :
  344. (HFONT)GetStockObject(DEFAULT_GUI_FONT));
  345. RECT spaceRect;
  346. SetRect(&spaceRect, WIDGET_SPACECX_UNITS, WIDGET_SPACECY_UNITS, 0, 0);
  347. Curtain_MapDialogRectHdc(hdc, &spaceRect);
  348. InflateRect(&clientRect, -spaceRect.left, -spaceRect.top);
  349. if (NULL != curtain->progressBitmap && 0 != curtain->imageSize.cx && 0 != curtain->imageSize.cy)
  350. {
  351. RECT imageRect;
  352. SetRect(&imageRect, 0, 0, curtain->imageSize.cx, curtain->imageSize.cy);
  353. Curtain_RectAlign(&imageRect, &clientRect, RA_HCENTER | RA_VCENTER);
  354. if (Curtain_PaintImage(curtain->progressBitmap, curtain->frameNumber, &imageRect, hdc,prcPaint))
  355. {
  356. SetRectRgn(regionPart, imageRect.left, imageRect.top, imageRect.right, imageRect.bottom);
  357. CombineRgn(regionPaint, regionPaint, regionPart, RGN_DIFF);
  358. }
  359. }
  360. if (NULL != curtain->pszTitle && 0 != curtain->titleSize.cx && 0 != curtain->titleSize.cy)
  361. {
  362. RECT textRect, paintRect;;
  363. SetRect(&textRect, 0, 0, curtain->titleSize.cx, curtain->titleSize.cy);
  364. Curtain_RectAlign(&textRect, &clientRect, RA_HCENTER | RA_TOP | RA_FITHORZ);
  365. if (IntersectRect(&paintRect, &textRect, prcPaint))
  366. {
  367. INT cchText = lstrlen(curtain->pszTitle);
  368. if (ExtTextOut(hdc, textRect.left, textRect.top, ETO_CLIPPED | ETO_OPAQUE, &paintRect, curtain->pszTitle, cchText, NULL))
  369. {
  370. SetRectRgn(regionPart, paintRect.left, paintRect.top, paintRect.right, paintRect.bottom);
  371. CombineRgn(regionPaint, regionPaint, regionPart, RGN_DIFF);
  372. }
  373. }
  374. }
  375. if (NULL != curtain->pszOperation && 0 != curtain->operationSize.cx && 0 != curtain->operationSize.cy)
  376. {
  377. RECT textRect, paintRect;
  378. SetRect(&textRect, 0, 0, curtain->operationSize.cx, curtain->operationSize.cy);
  379. Curtain_RectAlign(&textRect, &clientRect, RA_HCENTER | RA_BOTTOM | RA_FITHORZ);
  380. if (IntersectRect(&paintRect, &textRect, prcPaint))
  381. {
  382. INT cchText = lstrlen(curtain->pszOperation);
  383. if (ExtTextOut(hdc, textRect.left, textRect.top, ETO_CLIPPED | ETO_OPAQUE, &paintRect, curtain->pszOperation, cchText, NULL))
  384. {
  385. SetRectRgn(regionPart, paintRect.left, paintRect.top, paintRect.right, paintRect.bottom);
  386. CombineRgn(regionPaint, regionPaint, regionPart, RGN_DIFF);
  387. }
  388. }
  389. }
  390. SelectObject(hdc, originalFont);
  391. if (originalBk != curtain->rgbBk) SetBkColor(hdc, originalBk);
  392. if (originalFg != curtain->rgbFg) SetTextColor(hdc, originalFg);
  393. if (NULL != fErase)
  394. Curtain_PaintWidgetBack(curtain, hdc, regionPaint);
  395. DeleteObject(regionPaint);
  396. DeleteObject(regionPart);
  397. return TRUE;
  398. }
  399. static void Curtain_Paint(HWND hwnd, HDC hdc, const RECT *prcPaint, BOOL fErase)
  400. {
  401. CURTAIN *curtain = GetCurtain(hwnd);
  402. if (NULL == curtain) return;
  403. HRGN regionPaint = CreateRectRgnIndirect(prcPaint);
  404. if (!IsRectEmpty(&curtain->widgetRect))
  405. {
  406. RECT widgetPaintRect;
  407. if (IntersectRect(&widgetPaintRect, &curtain->widgetRect, prcPaint) &&
  408. FALSE != Curtain_PaintWidget(curtain, hdc, &widgetPaintRect, fErase))
  409. {
  410. HRGN regionWidget = CreateRectRgnIndirect(&widgetPaintRect);
  411. CombineRgn(regionPaint, regionPaint, regionWidget, RGN_DIFF);
  412. DeleteObject(regionWidget);
  413. }
  414. }
  415. if (FALSE != fErase)
  416. {
  417. if (NULL == curtain->backBrush)
  418. {
  419. curtain->backBrush = CreateSolidBrush(Curtain_GetColor(WADLG_ITEMBG, COLOR_WINDOW));
  420. }
  421. FillRgn(hdc, regionPaint, curtain->backBrush);
  422. }
  423. DeleteObject(regionPaint);
  424. }
  425. static void CALLBACK Curtain_AnimateTimerElpased(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
  426. {
  427. CURTAIN *curtain = GetCurtain(hwnd);
  428. if (NULL == curtain)
  429. {
  430. KillTimer(hwnd, idEvent);
  431. return;
  432. }
  433. if (NULL != curtain->progressBitmap && 0 != curtain->imageSize.cx && 0 != curtain->imageSize.cy)
  434. {
  435. curtain->frameNumber++;
  436. if (curtain->frameNumber >= PROGRESS_FRAMECOUNT)
  437. curtain->frameNumber = 0;
  438. RECT imageRect;
  439. SetRect(&imageRect, 0, 0, curtain->imageSize.cx, curtain->imageSize.cy);
  440. Curtain_RectAlign(&imageRect, &curtain->widgetRect, RA_HCENTER | RA_VCENTER);
  441. InvalidateRect(hwnd, &imageRect, FALSE);
  442. }
  443. }
  444. static LRESULT Curtain_OnCreate(HWND hwnd, CREATESTRUCT *pcs)
  445. {
  446. CURTAIN *curtain = (CURTAIN*)calloc(1, sizeof(CURTAIN));
  447. if (NULL != curtain)
  448. {
  449. SetLastError(ERROR_SUCCESS);
  450. if (!SetWindowLongPtr(hwnd, 0, (LONGX86)(LONG_PTR)curtain) && ERROR_SUCCESS != GetLastError())
  451. {
  452. free(curtain);
  453. curtain = NULL;
  454. }
  455. }
  456. if (NULL == curtain)
  457. {
  458. DestroyWindow(hwnd);
  459. return -1;
  460. }
  461. SetTimer(hwnd, ANIMATETIMER_ID, ANIMATETIMER_INTERVAL, Curtain_AnimateTimerElpased);
  462. return 0;
  463. }
  464. static void Curtain_OnDestroy(HWND hwnd)
  465. {
  466. CURTAIN *curtain = GetCurtain(hwnd);
  467. SetWindowLongPtr(hwnd, 0, 0L);
  468. if (NULL != curtain)
  469. {
  470. if (NULL != curtain->pszTitle)
  471. Plugin_FreeString(curtain->pszTitle);
  472. if (NULL != curtain->pszOperation)
  473. Plugin_FreeString(curtain->pszOperation);
  474. if (NULL != curtain->backBrush)
  475. DeleteObject(curtain->backBrush);
  476. if (NULL != curtain->widgetBrush)
  477. DeleteObject(curtain->widgetBrush);
  478. if (NULL != curtain->frameBrush)
  479. DeleteObject(curtain->frameBrush);
  480. if (NULL != curtain->progressBitmap)
  481. DeleteObject(curtain->progressBitmap);
  482. free(curtain);
  483. }
  484. }
  485. static void Curtain_OnPaint(HWND hwnd)
  486. {
  487. PAINTSTRUCT ps = {0};
  488. if (BeginPaint(hwnd, &ps))
  489. {
  490. if (ps.rcPaint.left != ps.rcPaint.right)
  491. Curtain_Paint(hwnd, ps.hdc, &ps.rcPaint, ps.fErase);
  492. EndPaint(hwnd, &ps);
  493. }
  494. }
  495. static void Curtain_OnPrintClient(HWND hwnd, HDC hdc, UINT options)
  496. {
  497. RECT clientRect;
  498. if (GetClientRect(hwnd, &clientRect))
  499. Curtain_Paint(hwnd, hdc, &clientRect, 0 != (PRF_ERASEBKGND & options));
  500. }
  501. static void Curtain_OnWindowPosChanged(HWND hwnd, WINDOWPOS *pwp)
  502. {
  503. if (SWP_NOSIZE == ((SWP_NOSIZE | SWP_FRAMECHANGED) & pwp->flags))
  504. return;
  505. Curtain_UpdateLayout(hwnd, 0 == (SWP_NOREDRAW & pwp->flags));
  506. }
  507. static void Curtain_OnCommand(HWND hwnd, INT controlId, INT eventId, HWND hControl)
  508. {
  509. }
  510. static BOOL Curtain_OnSetText(HWND hwnd, LPCWSTR pszText)
  511. {
  512. CURTAIN *curtain = GetCurtain(hwnd);
  513. if (NULL == curtain) return FALSE;
  514. Curtain_SetPluginString(&curtain->pszTitle, pszText);
  515. InvalidateRect(hwnd, &curtain->widgetRect, FALSE);
  516. return TRUE;
  517. }
  518. static INT Curtain_OnGetText(HWND hwnd, LPWSTR pszBuffer, INT cchBufferMax)
  519. {
  520. if (NULL == pszBuffer || cchBufferMax)
  521. return 0;
  522. pszBuffer[0] = L'\0';
  523. CURTAIN *curtain = GetCurtain(hwnd);
  524. if (NULL == curtain || NULL == curtain->pszTitle) return FALSE;
  525. INT cchTitle = lstrlenW(curtain->pszTitle);
  526. if (cchTitle > 0)
  527. {
  528. if (cchTitle >= cchBufferMax)
  529. cchTitle = (cchBufferMax - 1);
  530. StringCchCopyN(pszBuffer, cchBufferMax, curtain->pszTitle, cchTitle);
  531. }
  532. return cchTitle;
  533. }
  534. static INT Curtain_OnGetTextLength(HWND hwnd)
  535. {
  536. CURTAIN *curtain = GetCurtain(hwnd);
  537. if (NULL == curtain || NULL == curtain->pszTitle) return FALSE;
  538. return lstrlenW(curtain->pszTitle);
  539. }
  540. static BOOL Curtain_OnSetOperationText(HWND hwnd, LPCWSTR pszText)
  541. {
  542. CURTAIN *curtain = GetCurtain(hwnd);
  543. if (NULL == curtain) return FALSE;
  544. Curtain_SetPluginString(&curtain->pszOperation, pszText);
  545. InvalidateRect(hwnd, &curtain->widgetRect, FALSE);
  546. return TRUE;
  547. }
  548. static void Curtain_OnUpdateSkin(HWND hwnd, BOOL fRedraw)
  549. {
  550. CURTAIN *curtain = GetCurtain(hwnd);
  551. if (NULL == curtain) return;
  552. ifc_skinhelper *skinHelper = NULL;
  553. if (FAILED(Plugin_GetSkinHelper(&skinHelper)))
  554. skinHelper = NULL;
  555. curtain->textFont = (NULL != skinHelper) ? skinHelper->GetFont() : NULL;
  556. if (NULL != curtain->backBrush)
  557. {
  558. DeleteObject(curtain->backBrush);
  559. curtain->backBrush = NULL;
  560. }
  561. if (NULL != curtain->widgetBrush)
  562. {
  563. DeleteObject(curtain->widgetBrush);
  564. curtain->widgetBrush = NULL;
  565. }
  566. if (NULL != curtain->frameBrush)
  567. {
  568. DeleteObject(curtain->frameBrush);
  569. curtain->frameBrush = NULL;
  570. }
  571. if (NULL != curtain->progressBitmap)
  572. DeleteObject(curtain->progressBitmap);
  573. if (NULL == skinHelper || FAILED(skinHelper->GetColor(WADLG_WNDBG, &curtain->rgbBk)))
  574. curtain->rgbBk = GetSysColor(COLOR_WINDOW);
  575. if (NULL == skinHelper || FAILED(skinHelper->GetColor(WADLG_WNDFG, &curtain->rgbFg)))
  576. curtain->rgbFg = GetSysColor(COLOR_WINDOWTEXT);
  577. if (NULL != skinHelper)
  578. skinHelper->Release();
  579. curtain->progressBitmap = Curtain_LoadImage(hwnd, PROGRESS_FRAMECOUNT, curtain->rgbBk, curtain->rgbFg);
  580. Curtain_UpdateLayout(hwnd, fRedraw);
  581. }
  582. static LRESULT Curtain_OnGetFont(HWND hwnd)
  583. {
  584. CURTAIN *curtain = GetCurtain(hwnd);
  585. if (NULL == curtain) return NULL;
  586. return (LRESULT)curtain->textFont;
  587. }
  588. static void Curtain_OnSetFont(HWND hwnd, HFONT hFont, BOOL fRedraw)
  589. {
  590. CURTAIN *curtain = GetCurtain(hwnd);
  591. if (NULL == curtain) return;
  592. curtain->textFont = hFont;
  593. if (FALSE != fRedraw)
  594. InvalidateRect(hwnd, &curtain->widgetRect, FALSE);
  595. }
  596. static LRESULT CALLBACK Curtain_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  597. {
  598. switch (uMsg)
  599. {
  600. case WM_CREATE: return Curtain_OnCreate(hwnd, (CREATESTRUCT*)lParam);
  601. case WM_DESTROY: Curtain_OnDestroy(hwnd); break;
  602. case WM_PAINT: Curtain_OnPaint(hwnd); return 0;
  603. case WM_PRINTCLIENT: Curtain_OnPrintClient(hwnd, (HDC)wParam, (UINT)lParam); return 0;
  604. case WM_ERASEBKGND: return 0;
  605. case WM_WINDOWPOSCHANGED: Curtain_OnWindowPosChanged(hwnd, (WINDOWPOS*)lParam); return 0;
  606. case WM_SETTEXT: return Curtain_OnSetText(hwnd, (LPCWSTR)lParam);
  607. case WM_GETTEXT: return Curtain_OnGetText(hwnd, (LPWSTR)lParam, (INT)wParam);
  608. case WM_GETTEXTLENGTH: return Curtain_OnGetTextLength(hwnd);
  609. case WM_GETFONT: return Curtain_OnGetFont(hwnd);
  610. case WM_SETFONT: Curtain_OnSetFont(hwnd, (HFONT)wParam, (BOOL)LOWORD(lParam));
  611. case WM_COMMAND: Curtain_OnCommand(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break;
  612. case CWM_SETOPERATIONTEXT: return Curtain_OnSetOperationText(hwnd, (LPCWSTR)lParam);
  613. case CWM_UPDATESKIN: Curtain_OnUpdateSkin(hwnd, (BOOL)lParam); return 0;
  614. }
  615. return DefWindowProcW(hwnd, uMsg, wParam, lParam);
  616. }