skinnedbutton.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. #include "main.h"
  2. #include "./skinnedbutton.h"
  3. #include "../winamp/wa_dlg.h"
  4. #include "./skinning.h"
  5. #include "./stockobjects.h"
  6. #include "./colors.h"
  7. #include "./ml_imagefilter.h"
  8. #include "./ml_imagelist.h"
  9. #include "./resource.h"
  10. #include <math.h>
  11. #include <strsafe.h>
  12. #define MARGIN_TOP 2
  13. #define MARGIN_BOTTOM 2
  14. #define MARGIN_LEFT 6
  15. #define MARGIN_RIGHT 6
  16. #define IMAGE_SPACE 3
  17. #define SPLITAREA_WIDTH 19
  18. #define SPLITAREA_SPACE -5
  19. #define SPLITAREA_SPACE_ALT -3
  20. #define IMAGEFILTER_BLEND 0x00
  21. #define IMAGEFILTER_BLENDPLUSCOLOR 0x01
  22. // internaly used button styles
  23. #define SWBS_SPLITSETMANUAL 0x10000000
  24. #define SWBS_SPLITHOVER 0x20000000
  25. #define SWBS_BUTTONHOVER 0x40000000
  26. // additional colors
  27. typedef struct _EXTENDEDCOLORTABLE
  28. {
  29. BOOL updateRequired;
  30. COLORREF rgbTextBk;
  31. BOOL bTextBkUnified;
  32. COLORREF rgbTextBkDown;
  33. BOOL bTextBkDownUnified;
  34. COLORREF rgbTextDisabled;
  35. COLORREF rgbTextDisabled2; // disabled based on WNDBG
  36. COLORREF rgbText15;
  37. COLORREF rgbText15Down;
  38. COLORREF rgbText75;
  39. COLORREF rgbText75Down;
  40. COLORREF rgbToolBkHigh;
  41. COLORREF rgbToolBkPressed;
  42. } EXTENDEDCOLORTABLE;
  43. typedef struct _IMAGEFILTERPARAM
  44. {
  45. BUTTONPAINTSTRUCT *pbps;
  46. RECT *prcImage;
  47. } IMAGEFILTERPARAM;
  48. extern HMLIMGFLTRMNGR hmlifMngr; // default gen_ml fitler manager
  49. static EXTENDEDCOLORTABLE extendedColors = { TRUE, 0, };
  50. static HWND hoverHWND = NULL;
  51. static BOOL hoverSplit = FALSE;
  52. static HMLIMGLST hmlilSplitter = NULL;
  53. static IMAGEFILTERPARAM imagefilterParam;
  54. static HWND lButtonDownHWND = NULL;
  55. static HMLIMGLST CreateSplitterImageList()
  56. {
  57. HMLIMGLST hmlil = MLImageListI_Create(6, 4, MLILC_COLOR32, 2, 1, 3, hmlifMngr);
  58. if (hmlil)
  59. {
  60. MLIMAGESOURCE_I mlis = {0};
  61. mlis.type = SRC_TYPE_PNG_I;
  62. mlis.hInst = plugin.hDllInstance;
  63. mlis.bpp = 32;
  64. mlis.flags = ISF_FORCE_BPP_I;
  65. mlis.lpszName = MAKEINTRESOURCEW(IDB_SPLITARROW);
  66. MLImageListI_Add(hmlil, &mlis, MLIF_BUTTONBLENDPLUSCOLOR_UID, 0);
  67. mlis.lpszName = MAKEINTRESOURCEW(IDB_SPLITARROW_PRESSED);
  68. MLImageListI_Add(hmlil, &mlis, MLIF_BUTTONBLENDPLUSCOLOR_UID, 0);
  69. }
  70. return hmlil;
  71. }
  72. static void DrawButtonImage(HMLIMGLST hmlil, INT hmlilIndex, RECT *prcImage, BUTTONPAINTSTRUCT *pbps)
  73. {
  74. INT offset;
  75. HIMAGELIST himl = MLImageListI_GetRealList(hmlil);
  76. imagefilterParam.pbps = pbps;
  77. imagefilterParam.prcImage = prcImage;
  78. if (BST_PUSHED & pbps->buttonState)
  79. offset = 10000;
  80. else if (WS_DISABLED & pbps->windowStyle)
  81. offset = 20000;
  82. else if (SWBS_BUTTONHOVER & pbps->skinnedStyle)
  83. offset = 30000;
  84. else
  85. offset = 0;
  86. BOOL bUN;
  87. if (SWBS_TOOLBAR & pbps->skinnedStyle)
  88. {
  89. bUN = TRUE;
  90. offset += 40000;
  91. if (offset == 40000 && (WS_BORDER & pbps->windowStyle))
  92. {
  93. offset = 40000 + 30000;
  94. }
  95. }
  96. else
  97. {
  98. bUN = ((BST_PUSHED & pbps->buttonState) ?
  99. extendedColors.bTextBkDownUnified :
  100. extendedColors.bTextBkUnified);
  101. }
  102. COLORREF colorBk, colorFg;
  103. colorFg = (COLORREF)(INT_PTR)WADlg_getBitmap();
  104. colorBk = (COLORREF)MAKELONG(prcImage->bottom - prcImage->top, ((bUN) ? prcImage->top : 0) + offset);
  105. INT index = MLImageListI_GetRealIndex(hmlil, hmlilIndex, colorBk, colorFg);
  106. if (-1 != index)
  107. ImageList_Draw(himl, index, pbps->hdc, prcImage->left, prcImage->top, ILD_NORMAL);
  108. }
  109. typedef struct _DIBDATA
  110. {
  111. BYTE *pData;
  112. INT bpp;
  113. INT cx;
  114. INT cy;
  115. INT step;
  116. LONG pitch;
  117. BOOL bFree;
  118. } DIBDATA;
  119. static BYTE* GetButtonDib(DIBDATA *pdd, BOOL bPressed)
  120. {
  121. BITMAPINFOHEADER bi;
  122. HBITMAP hbmp;
  123. if (!pdd) return NULL;
  124. ZeroMemory(pdd, sizeof(DIBDATA));
  125. hbmp = WADlg_getBitmap();
  126. if (!hbmp) return NULL;
  127. HDC hdc = (HDC)MlStockObjects_Get(CACHED_DC);
  128. ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
  129. bi.biSize = sizeof(BITMAPINFOHEADER);
  130. if (GetDIBits(hdc, hbmp, 0, 0, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS))
  131. {
  132. bi.biCompression = BI_RGB;
  133. pdd->bFree = TRUE;
  134. pdd->step = (bi.biBitCount>>3);
  135. pdd->pitch = bi.biWidth * pdd->step;
  136. while (pdd->pitch%4) pdd->pitch++;
  137. pdd->pData = (BYTE*)malloc(7 * pdd->pitch);
  138. pdd->cx = bi.biWidth;
  139. INT o = (bPressed) ? 15 : 0;
  140. pdd->cy = GetDIBits(hdc, hbmp,
  141. ((bi.biHeight > 0) ? (bi.biHeight - (11 + o)) : (4 + o)), 7,
  142. pdd->pData, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
  143. if (!pdd->cy)
  144. {
  145. free(pdd->pData);
  146. pdd->pData = NULL;
  147. }
  148. }
  149. return pdd->pData;
  150. }
  151. COLORREF SkinnedButton::GetButtonBkColor(BOOL bPressed, BOOL *pbUnified)
  152. {
  153. DIBDATA dib;
  154. BOOL bUnified = TRUE;
  155. ULONG r(0), g(0), b(0);
  156. int cy;
  157. if (GetButtonDib(&dib, bPressed))
  158. {
  159. cy = dib.cy;
  160. BYTE *line = dib.pData + (dib.step * 4);
  161. DWORD p = *(DWORD*)line;
  162. for (; cy-- != 0; line += dib.pitch)
  163. {
  164. r += line[2];
  165. g += line[1];
  166. b += line[0];
  167. if (bUnified && p != *(DWORD*)line) bUnified = FALSE;
  168. }
  169. if (dib.bFree) free(dib.pData);
  170. }
  171. if (pbUnified) *pbUnified = bUnified;
  172. cy = (dib.cy ? dib.cy : 1);
  173. return RGB(r/cy, g/cy, b/cy);
  174. }
  175. static void UpdateExtendedColorTable()
  176. {
  177. COLORREF fg, bk;
  178. float r;
  179. extendedColors.updateRequired = FALSE;
  180. extendedColors.rgbTextBk = SkinnedButton::GetButtonBkColor(FALSE, &extendedColors.bTextBkUnified);
  181. extendedColors.rgbTextBkDown = SkinnedButton::GetButtonBkColor(TRUE, &extendedColors.bTextBkDownUnified);
  182. extendedColors.rgbTextDisabled = WADlg_getColor(WADLG_BUTTONFG);
  183. r = 77;
  184. do
  185. {
  186. r -= 1;
  187. fg = BlendColors(extendedColors.rgbTextDisabled, extendedColors.rgbTextBk, (COLORREF)r);
  188. } while (GetColorDistance(fg, extendedColors.rgbTextBk) > 70);
  189. extendedColors.rgbTextDisabled = fg;
  190. extendedColors.rgbTextDisabled2 = WADlg_getColor(WADLG_WNDFG);
  191. bk = WADlg_getColor(WADLG_WNDBG);
  192. r = 77;
  193. do
  194. {
  195. r -= 1;
  196. fg = BlendColors(extendedColors.rgbTextDisabled2, bk, (COLORREF)r);
  197. } while (GetColorDistance(fg, bk) > 70);
  198. extendedColors.rgbTextDisabled2 = fg;
  199. fg = WADlg_getColor(WADLG_BUTTONFG);
  200. extendedColors.rgbText15 = BlendColors(fg, extendedColors.rgbTextBk, 40);
  201. extendedColors.rgbText75 = BlendColors(fg, extendedColors.rgbTextBk, 191);
  202. extendedColors.rgbText15Down = BlendColors(fg, extendedColors.rgbTextBkDown, 40);
  203. extendedColors.rgbText75Down = BlendColors(fg, extendedColors.rgbTextBkDown, 191);
  204. fg = WADlg_getColor(WADLG_HILITE);
  205. extendedColors.rgbToolBkHigh = BlendColors(fg, bk, 64);
  206. extendedColors.rgbToolBkPressed = BlendColors(fg, bk, 127);
  207. }
  208. SkinnedButton::SkinnedButton(void) : SkinnedWnd(FALSE)
  209. {
  210. ZeroMemory(&imagelist, sizeof(MLBUTTONIMAGELIST));
  211. }
  212. SkinnedButton::~SkinnedButton(void)
  213. {
  214. }
  215. BOOL SkinnedButton::Attach(HWND hwndButton)
  216. {
  217. if(!SkinnedWnd::Attach(hwndButton)) return FALSE;
  218. SetType(SKINNEDWND_TYPE_BUTTON);
  219. SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
  220. return TRUE;
  221. }
  222. void SkinnedButton::OnSkinChanged(BOOL bNotifyChildren, BOOL bRedraw)
  223. {
  224. extendedColors.updateRequired = TRUE;
  225. __super::OnSkinChanged(bNotifyChildren, bRedraw);
  226. }
  227. static void PushButton_DrawBackground(BUTTONPAINTSTRUCT *pbps)
  228. {
  229. if (SWBS_TOOLBAR & pbps->skinnedStyle)
  230. {
  231. SetBkColor(pbps->hdc, pbps->rgbTextBk);
  232. ExtTextOutW(pbps->hdc, 0, 0, ETO_OPAQUE, &pbps->rcPaint, NULL, 0, NULL);
  233. if (WS_BORDER & pbps->windowStyle) FrameRect(pbps->hdc, &pbps->rcClient, (HBRUSH)MlStockObjects_Get(HILITE_BRUSH));
  234. }
  235. else
  236. {
  237. HBITMAP hbmp = WADlg_getBitmap();
  238. if (hbmp)
  239. {
  240. INT stretchModeOld;
  241. INT yoffs = (BST_PUSHED & pbps->buttonState) ? 15 : 0;
  242. HDC hdcSrc = (HDC)MlStockObjects_Get(CACHED_DC);
  243. HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcSrc, hbmp);
  244. SetBrushOrgEx(pbps->hdc, pbps->rcClient.left, pbps->rcClient.top, NULL);
  245. stretchModeOld = SetStretchBltMode(pbps->hdc, COLORONCOLOR);
  246. if (pbps->rcPaint.top <=4)
  247. {
  248. if (pbps->rcPaint.left <= 4)
  249. BitBlt(pbps->hdc, pbps->rcPaint.left, pbps->rcPaint.top, 4 - pbps->rcPaint.left, 4 - pbps->rcPaint.top,
  250. hdcSrc, pbps->rcPaint.left, pbps->rcPaint.top + yoffs, SRCCOPY); // top left
  251. if (pbps->rcPaint.right > 4 && pbps->rcPaint.left < pbps->rcClient.right - 4)
  252. StretchBlt(pbps->hdc, 4, pbps->rcClient.top, pbps->rcClient.right -4 -4, 4,
  253. hdcSrc, 4, 0 + yoffs, 47-4-4, 4, SRCCOPY); // top center
  254. if (pbps->rcPaint.right >= pbps->rcClient.right - 4)
  255. BitBlt(pbps->hdc, pbps->rcClient.right - 4, pbps->rcPaint.top, 4, 4 - pbps->rcPaint.top,
  256. hdcSrc, 47 - 4, pbps->rcPaint.top + yoffs, SRCCOPY); // top right
  257. }
  258. if (pbps->rcPaint.left <= 4)
  259. StretchBlt(pbps->hdc, 0, 4, 4, pbps->rcClient.bottom - 4 -4, hdcSrc, 0, 4 + yoffs, 4, 15-4-4, SRCCOPY); // left edge
  260. if (pbps->rcPaint.right >= pbps->rcClient.right - 4)
  261. StretchBlt(pbps->hdc, pbps->rcClient.right - 4, pbps->rcClient.top + 4, 4, pbps->rcClient.bottom - 4 - 4, hdcSrc, 47-4,4+yoffs, 4, 15-4-4,SRCCOPY); // right edge
  262. // center
  263. if ((pbps->rcPaint.right > 4 && pbps->rcPaint.left < pbps->rcClient.right - 4) &&
  264. (pbps->rcPaint.bottom > 4 && pbps->rcPaint.top < pbps->rcClient.bottom- 4))
  265. StretchBlt(pbps->hdc, 4, 4, pbps->rcClient.right-4-4, pbps->rcClient.bottom -4 -4,
  266. hdcSrc, 4,4+yoffs,47-4-4,15-4-4,SRCCOPY);
  267. if (pbps->rcPaint.bottom >= pbps->rcClient.bottom - 4)
  268. {
  269. if (pbps->rcPaint.left <= 4)
  270. BitBlt(pbps->hdc, 0, pbps->rcClient.bottom-4,4,4, hdcSrc, 0, 15-4+yoffs, SRCCOPY); // bottom left
  271. if (pbps->rcPaint.right > 4 && pbps->rcPaint.left < pbps->rcClient.right - 4)
  272. StretchBlt(pbps->hdc, 4, pbps->rcClient.bottom-4, pbps->rcClient.right-4-4,4, hdcSrc, 4,15-4+yoffs,47-4-4,4,SRCCOPY); // bottom center
  273. if (pbps->rcPaint.right >= pbps->rcClient.right - 4)
  274. BitBlt(pbps->hdc, pbps->rcClient.right - 4, pbps->rcClient.bottom - 4, 4, 4, hdcSrc, 47-4, 15-4+yoffs, SRCCOPY); // bottom right
  275. }
  276. SelectObject(hdcSrc, hbmpOld);
  277. if (COLORONCOLOR != stretchModeOld) SetStretchBltMode(pbps->hdc, stretchModeOld);
  278. }
  279. }
  280. }
  281. static void PushButton_CalulateRects(BUTTONPAINTSTRUCT *pbps, RECT *prcText, RECT *prcImage, INT imageWidth, INT imageHeight)
  282. {
  283. SetRect(prcText, 0, 0, 0, 0);
  284. SetRect(prcImage, 0, 0, 0, 0);
  285. if (!pbps->hImage && !pbps->cchText) return;
  286. if (0 != (0x0F & pbps->textFormat))
  287. {
  288. INT l, t;
  289. if (pbps->cchText)
  290. {
  291. if (FALSE == DrawTextW(pbps->hdc, pbps->szText, pbps->cchText, prcText,
  292. (~0x0F & pbps->textFormat) | DT_CALCRECT))
  293. {
  294. SetRectEmpty(prcText);
  295. }
  296. if (imageWidth) prcText->right += (imageWidth + IMAGE_SPACE);
  297. }
  298. else SetRect(prcText, 0, 0, imageWidth, imageHeight);
  299. if (DT_BOTTOM & pbps->textFormat) t = pbps->rcClient.bottom - prcText->bottom - MARGIN_BOTTOM;
  300. else if (DT_VCENTER & pbps->textFormat) t = pbps->rcClient.top + ((pbps->rcClient.bottom - pbps->rcClient.top) - prcText->bottom)/2;
  301. else t = pbps->rcClient.top + MARGIN_TOP;
  302. if (DT_RIGHT & pbps->textFormat)
  303. {
  304. l = pbps->rcClient.right - prcText->right;
  305. if (SWBS_DROPDOWNBUTTON & pbps->skinnedStyle)
  306. l -= (SPLITAREA_WIDTH + SPLITAREA_SPACE);
  307. if (SWBS_SPLITBUTTON & pbps->skinnedStyle)
  308. l -= (SPLITAREA_WIDTH + SPLITAREA_SPACE_ALT);
  309. else
  310. l -= MARGIN_RIGHT;
  311. }
  312. else if (DT_CENTER & pbps->textFormat)
  313. {
  314. int w = pbps->rcClient.right - pbps->rcClient.left;
  315. if (SWBS_DROPDOWNBUTTON & pbps->skinnedStyle)
  316. {
  317. w -= (SPLITAREA_WIDTH + SPLITAREA_SPACE);
  318. }
  319. else if (SWBS_SPLITBUTTON & pbps->skinnedStyle)
  320. {
  321. w -= (SPLITAREA_WIDTH + SPLITAREA_SPACE_ALT);
  322. }
  323. l = pbps->rcClient.left + (w - prcText->right)/2;
  324. }
  325. else
  326. l = pbps->rcClient.left + MARGIN_LEFT;
  327. if (BST_PUSHED & pbps->buttonState && (0 == (BS_PUSHLIKE & pbps->windowStyle)))
  328. {
  329. if (SWBS_DROPDOWNBUTTON & pbps->skinnedStyle) t++;
  330. else
  331. {
  332. l++;
  333. if (SWBS_TOOLBAR & pbps->skinnedStyle) t++;
  334. }
  335. }
  336. OffsetRect(prcText, l, t);
  337. }
  338. else
  339. {
  340. SetRect(prcText, MARGIN_LEFT , MARGIN_TOP,
  341. pbps->rcClient.right - MARGIN_RIGHT, pbps->rcClient.bottom - MARGIN_BOTTOM);
  342. if (0 != (BST_PUSHED & pbps->buttonState))
  343. prcText +=1;
  344. }
  345. // Fit Text Rect
  346. if (prcText->top < (pbps->rcClient.top + MARGIN_TOP))
  347. {
  348. LONG h = prcText->bottom - prcText->top;
  349. prcText->top = pbps->rcClient.top + MARGIN_TOP;
  350. if ((BST_PUSHED & pbps->buttonState) && (SWBS_DROPDOWNBUTTON & pbps->skinnedStyle)) prcText->top++;
  351. prcText->bottom = prcText->top + h;
  352. }
  353. if (prcText->bottom > (pbps->rcClient.bottom - MARGIN_BOTTOM)) prcText->bottom = pbps->rcClient.bottom - MARGIN_BOTTOM;
  354. if (prcText->left < (pbps->rcClient.left + MARGIN_LEFT))
  355. {
  356. LONG w = prcText->right - prcText->left;
  357. prcText->left = pbps->rcClient.left + MARGIN_LEFT;
  358. if ((BST_PUSHED & pbps->buttonState) && 0 == (SWBS_DROPDOWNBUTTON & pbps->skinnedStyle)) prcText->left++;
  359. prcText->right = prcText->left + w - ((BST_PUSHED & pbps->buttonState) ? 1 : 0);
  360. }
  361. int lim = pbps->rcClient.right;
  362. if (SWBS_SPLITBUTTON & pbps->skinnedStyle)
  363. {
  364. lim -= (SPLITAREA_WIDTH + SPLITAREA_SPACE_ALT);
  365. if (BST_PUSHED & pbps->buttonState) lim++;
  366. }
  367. else if (SWBS_DROPDOWNBUTTON & pbps->skinnedStyle)
  368. lim -= (SPLITAREA_WIDTH + SPLITAREA_SPACE);
  369. else
  370. lim -= MARGIN_RIGHT;
  371. if (prcText->right > lim) prcText->right = lim;
  372. if (imageHeight && imageWidth)
  373. {
  374. INT top;
  375. if (prcText->left + imageWidth > prcText->right) imageWidth = prcText->right - prcText->left;
  376. top = prcText->top;
  377. if (prcText->top + imageHeight > prcText->bottom)
  378. {
  379. if ((DT_BOTTOM | DT_VCENTER) & pbps->textFormat)
  380. {
  381. if (DT_BOTTOM & pbps->textFormat) top = prcText->bottom -imageHeight;
  382. else top += ((prcText->bottom - prcText->top) - imageHeight) / 2;
  383. if (top < pbps->rcClient.top + MARGIN_TOP) top = pbps->rcClient.top + MARGIN_TOP;
  384. }
  385. if (top + imageHeight > (pbps->rcClient.bottom - MARGIN_BOTTOM)) imageHeight = pbps->rcClient.bottom - top - MARGIN_BOTTOM;
  386. }
  387. if ((DT_VCENTER & pbps->textFormat) && (prcText->bottom - top) > imageHeight)
  388. top += ((prcText->bottom - top) - imageHeight)/2;
  389. SetRect(prcImage, prcText->left, top, prcText->left + imageWidth, top + imageHeight);
  390. prcText->left += (imageWidth + IMAGE_SPACE);
  391. }
  392. }
  393. void SkinnedButton::DrawPushButton(BUTTONPAINTSTRUCT *pbps)
  394. {
  395. RECT rcText, rcImage;
  396. SetRect(&rcText, 0, 0, 0, 0);
  397. SetRect(&rcImage, 0, 0, 0, 0);
  398. if (pbps->hImage)
  399. {
  400. if (-1 == pbps->imageIndex)
  401. {
  402. BITMAP bi;
  403. if (GetObjectW(pbps->hImage, sizeof(BITMAP), &bi)) { rcImage.right = bi.bmWidth; rcImage.bottom = bi.bmHeight; }
  404. }
  405. else
  406. {
  407. MLImageListI_GetImageSize((HMLIMGLST)pbps->hImage, (INT*)&rcImage.right, (INT*)&rcImage.bottom);
  408. }
  409. }
  410. PushButton_CalulateRects(pbps, &rcText, &rcImage, rcImage.right, rcImage.bottom);
  411. PushButton_DrawBackground(pbps);
  412. IntersectClipRect(pbps->hdc, pbps->rcClient.left + MARGIN_LEFT, pbps->rcClient.top + MARGIN_TOP,
  413. pbps->rcClient.right - MARGIN_RIGHT, pbps->rcClient.bottom - MARGIN_BOTTOM);
  414. if (rcImage.left != rcImage.right)
  415. {
  416. if (-1 == pbps->imageIndex)
  417. {
  418. HDC hdcSrc = (HDC)MlStockObjects_Get(CACHED_DC);
  419. HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcSrc, pbps->hImage);
  420. BitBlt(pbps->hdc, rcImage.left, rcImage.top, rcImage.right - rcImage.left, rcImage.bottom - rcImage.top, hdcSrc, 0, 0, SRCCOPY);
  421. SelectObject(hdcSrc, hbmpOld);
  422. }
  423. else DrawButtonImage((HMLIMGLST)pbps->hImage, pbps->imageIndex, &rcImage, pbps);
  424. }
  425. if (0 != pbps->cchText) // draw text
  426. {
  427. INT bkModeOld = SetBkMode(pbps->hdc, TRANSPARENT);
  428. DrawTextW(pbps->hdc, pbps->szText, pbps->cchText, &rcText, (~0x0F & pbps->textFormat) | DT_WORD_ELLIPSIS | DT_NOCLIP);
  429. if (TRANSPARENT != bkModeOld) SetBkMode(pbps->hdc, bkModeOld);
  430. }
  431. if ((BST_FOCUS & pbps->buttonState) ||(SWBS_SPLITBUTTON & pbps->skinnedStyle))
  432. {
  433. HRGN rgn;
  434. rgn = CreateRectRgnIndirect(&pbps->rcClient);
  435. SelectClipRgn(pbps->hdc, rgn);
  436. DeleteObject(rgn);
  437. }
  438. if ((SWBS_SPLITBUTTON | SWBS_DROPDOWNBUTTON) & pbps->skinnedStyle)
  439. {
  440. if (NULL == hmlilSplitter) hmlilSplitter = CreateSplitterImageList();
  441. if (hmlilSplitter)
  442. {
  443. RECT rs;
  444. INT left = pbps->rcClient.right - SPLITAREA_WIDTH;
  445. if ((BST_PUSHED & pbps->buttonState) && 0 == (SWBS_DROPDOWNBUTTON & pbps->skinnedStyle)) left++;
  446. SetRect(&rs, 0, 0, 0, 0);
  447. MLImageListI_GetImageSize(hmlilSplitter, (INT*)&rs.right, (INT*)&rs.bottom);
  448. OffsetRect(&rs, left + (SPLITAREA_WIDTH - rs.right)/2 - (SPLITAREA_WIDTH - rs.right)%2,
  449. pbps->rcClient.top + (pbps->rcClient.bottom - pbps->rcClient.top - rs.bottom)/2 + 1 +
  450. ((SWBS_SPLITPRESSED & pbps->skinnedStyle) ? 1 : 0));
  451. if (rs.left > (pbps->rcClient.left + 4) && rs.top > (pbps->rcClient.top + 2))
  452. DrawButtonImage(hmlilSplitter, ((SWBS_SPLITPRESSED | SWBS_SPLITHOVER) & pbps->skinnedStyle) ? 1 : 0,
  453. &rs, pbps);
  454. if (SWBS_SPLITBUTTON & pbps->skinnedStyle)
  455. {
  456. HPEN pen = (HPEN)GetStockObject(DC_PEN);
  457. HPEN penOld = (HPEN)SelectObject(pbps->hdc, pen);
  458. SetDCPenColor(pbps->hdc, (BST_PUSHED & pbps->buttonState) ? extendedColors.rgbText75Down :
  459. ((WS_DISABLED & pbps->windowStyle)? extendedColors.rgbText15 : extendedColors.rgbText75));
  460. MoveToEx(pbps->hdc, left, pbps->rcClient.top + (MARGIN_TOP + 1), NULL);
  461. LineTo(pbps->hdc, left, pbps->rcClient.bottom - (MARGIN_BOTTOM + 1));
  462. left++;
  463. SetDCPenColor(pbps->hdc, (BST_PUSHED & pbps->buttonState) ? extendedColors.rgbText15Down : extendedColors.rgbText15);
  464. MoveToEx(pbps->hdc, left, pbps->rcClient.top + (MARGIN_TOP + 1), NULL);
  465. LineTo(pbps->hdc, left, pbps->rcClient.bottom - (MARGIN_BOTTOM + 1));
  466. if (SWBS_SPLITPRESSED & pbps->skinnedStyle)
  467. {
  468. MoveToEx(pbps->hdc, left, pbps->rcClient.top + (MARGIN_TOP + 1), NULL);
  469. LineTo(pbps->hdc, pbps->rcClient.right - 2, pbps->rcClient.top + (MARGIN_TOP + 1));
  470. }
  471. SelectObject(pbps->hdc, penOld);
  472. }
  473. }
  474. }
  475. if (BST_FOCUS & pbps->buttonState)
  476. {
  477. if (SWBS_TOOLBAR & pbps->skinnedStyle)
  478. {
  479. FrameRect(pbps->hdc, &pbps->rcClient, (HBRUSH)MlStockObjects_Get(HILITE_BRUSH));
  480. }
  481. else
  482. {
  483. COLORREF fg, bk;
  484. RECT rf;
  485. SetRect(&rf, pbps->rcClient.left + 2, pbps->rcClient.top + 2, pbps->rcClient.right - 2, pbps->rcClient.bottom - 2);
  486. if (SWBS_SPLITBUTTON & pbps->skinnedStyle) rf.right = pbps->rcClient.right - SPLITAREA_WIDTH - 1;
  487. fg = SetTextColor(pbps->hdc, GetSysColor(COLOR_WINDOWTEXT));
  488. bk = SetBkColor(pbps->hdc, GetSysColor(COLOR_WINDOW));
  489. DrawFocusRect(pbps->hdc, &rf);
  490. SetTextColor(pbps->hdc, fg);
  491. SetBkColor(pbps->hdc, bk);
  492. }
  493. }
  494. }
  495. void SkinnedButton::DrawGroupBox(BUTTONPAINTSTRUCT *pbps)
  496. {
  497. }
  498. void SkinnedButton::DrawCheckBox(BUTTONPAINTSTRUCT *pbps)
  499. {
  500. ExtTextOutW(pbps->hdc, 0, 0, ETO_OPAQUE, &pbps->rcPaint, L"", 0, NULL);
  501. if (pbps->cchText) DrawTextW(pbps->hdc, pbps->szText, pbps->cchText, &pbps->rcClient, pbps->textFormat);
  502. }
  503. void SkinnedButton::DrawRadioButton(BUTTONPAINTSTRUCT *pbps)
  504. {
  505. ExtTextOutW(pbps->hdc, 0, 0, ETO_OPAQUE, &pbps->rcPaint, L"", 0, NULL);
  506. if (pbps->cchText) DrawTextW(pbps->hdc, pbps->szText, pbps->cchText, &pbps->rcClient, pbps->textFormat);
  507. }
  508. void SkinnedButton::DrawButton(BUTTONPAINTSTRUCT *pbps)
  509. {
  510. HFONT hfo(NULL);
  511. COLORREF rgbTextOld, rgbTextBkOld;
  512. if (pbps->hFont) hfo = (HFONT)SelectObject(pbps->hdc, pbps->hFont);
  513. rgbTextOld = SetTextColor(pbps->hdc, pbps->rgbText);
  514. rgbTextBkOld = SetBkColor(pbps->hdc, pbps->rgbTextBk);
  515. INT type;
  516. if (BS_PUSHLIKE & pbps->windowStyle) type = BS_PUSHBUTTON;
  517. else type = (BS_TYPEMASK & pbps->windowStyle);
  518. switch(type)
  519. {
  520. case BS_3STATE:
  521. case BS_AUTO3STATE:
  522. case BS_AUTOCHECKBOX:
  523. case BS_CHECKBOX:
  524. DrawCheckBox(pbps);
  525. break;
  526. case BS_RADIOBUTTON:
  527. case BS_AUTORADIOBUTTON:
  528. DrawRadioButton(pbps);
  529. break;
  530. case BS_GROUPBOX:
  531. DrawGroupBox(pbps);
  532. break;
  533. case BS_PUSHBUTTON:
  534. case BS_DEFPUSHBUTTON:
  535. DrawPushButton(pbps);
  536. break;
  537. }
  538. SetTextColor(pbps->hdc, rgbTextOld);
  539. SetBkColor(pbps->hdc, rgbTextBkOld);
  540. if (NULL != hfo) SelectObject(pbps->hdc, hfo);
  541. }
  542. void SkinnedButton::PrePaint(HWND hwnd, BUTTONPAINTSTRUCT *pbps, UINT skinStyle, UINT uiState)
  543. {
  544. GetClientRect(hwnd, &pbps->rcClient);
  545. pbps->windowStyle = (UINT)GetWindowLongPtrW(hwnd, GWL_STYLE);
  546. pbps->buttonState = (UINT)SendMessageW(hwnd, BM_GETSTATE, 0, 0L);
  547. pbps->skinnedStyle = skinStyle;
  548. if (hoverHWND == hwnd)
  549. {
  550. if (hoverSplit) pbps->skinnedStyle |= SWBS_SPLITHOVER;
  551. else pbps->skinnedStyle |= SWBS_BUTTONHOVER;
  552. }
  553. if ((BST_FOCUS & pbps->buttonState) && (0x01/*UISF_HIDEFOCUS*/ & uiState))
  554. {
  555. pbps->buttonState &= ~BST_FOCUS;
  556. }
  557. if (SWBS_DROPDOWNBUTTON & pbps->skinnedStyle)
  558. {
  559. if (hoverHWND == hwnd) pbps->skinnedStyle |= SWBS_BUTTONHOVER;
  560. if (SWBS_SPLITPRESSED & pbps->skinnedStyle) pbps->buttonState |= BST_PUSHED;
  561. }
  562. pbps->textFormat = 0;
  563. pbps->cchText = (INT)SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0L);
  564. if (!IsWindowEnabled(hwnd)) pbps->windowStyle |= WS_DISABLED;
  565. if (extendedColors.updateRequired) UpdateExtendedColorTable();
  566. if (BS_OWNERDRAW == (BS_TYPEMASK & pbps->windowStyle)) pbps->windowStyle = ((pbps->windowStyle & ~BS_OWNERDRAW) | BS_PUSHBUTTON);
  567. INT type;
  568. if (BS_PUSHLIKE & pbps->windowStyle) type = BS_PUSHBUTTON;
  569. else type = (BS_TYPEMASK & pbps->windowStyle);
  570. switch(type)
  571. {
  572. case BS_PUSHBUTTON:
  573. case BS_DEFPUSHBUTTON:
  574. if (SWBS_TOOLBAR & pbps->skinnedStyle)
  575. {
  576. BOOL bChecked = (BS_PUSHLIKE & pbps->windowStyle) ? (BST_CHECKED == SendMessageW(hwnd, BM_GETCHECK, 0, 0L)) : FALSE;
  577. if ((BST_PUSHED & pbps->buttonState) || hoverHWND == hwnd || bChecked) pbps->windowStyle |= WS_BORDER;
  578. if (BST_PUSHED & pbps->buttonState)
  579. pbps->rgbTextBk = extendedColors.rgbToolBkPressed;
  580. else if (hoverHWND == hwnd || bChecked) pbps->rgbTextBk = extendedColors.rgbToolBkHigh;
  581. else pbps->rgbTextBk = WADlg_getColor(WADLG_WNDBG);
  582. pbps->rgbText = WADlg_getColor(WADLG_WNDFG);
  583. }
  584. else
  585. {
  586. pbps->rgbTextBk = (BST_PUSHED & pbps->buttonState) ? extendedColors.rgbTextBkDown : extendedColors.rgbTextBk;
  587. pbps->rgbText = (WS_DISABLED & pbps->windowStyle) ? extendedColors.rgbTextDisabled : WADlg_getColor(WADLG_BUTTONFG);
  588. }
  589. break;
  590. default:
  591. pbps->rgbTextBk = WADlg_getColor(WADLG_WNDBG);
  592. pbps->rgbText = (WS_DISABLED & pbps->windowStyle) ? extendedColors.rgbTextDisabled2 : WADlg_getColor(WADLG_WNDFG);
  593. break;
  594. }
  595. if (pbps->cchText)
  596. {
  597. if (pbps->cchText > BUTTON_TEXT_MAX) pbps->cchText = BUTTON_TEXT_MAX;
  598. SendMessageW(hwnd, WM_GETTEXT, (WPARAM)BUTTON_TEXT_MAX, (LPARAM)pbps->szText);
  599. pbps->hFont = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0L);
  600. if (NULL == pbps->hFont) pbps->hFont = (HFONT)MlStockObjects_Get(DEFAULT_FONT);
  601. if ( 0 == (BS_MULTILINE & pbps->windowStyle)) pbps->textFormat |= DT_SINGLELINE;
  602. if (0x02/*UISF_HIDEACCEL*/ & uiState) pbps->textFormat |= 0x00100000 /*DT_HIDEPREFIX*/;
  603. }
  604. MLBUTTONIMAGELIST buttonIL;
  605. if(MLSkinnedButton_GetImageList(hwnd, &buttonIL) && buttonIL.hmlil)
  606. {
  607. pbps->imageIndex = -1;
  608. if (WS_DISABLED & pbps->windowStyle) pbps->imageIndex = buttonIL.disabledIndex;
  609. else if (BST_PUSHED & pbps->buttonState) pbps->imageIndex = buttonIL.pressedIndex;
  610. if (-1 == pbps->imageIndex)
  611. pbps->imageIndex = ((SWBS_BUTTONHOVER & pbps->skinnedStyle) && -1 != buttonIL.hoverIndex) ? buttonIL.hoverIndex : buttonIL.normalIndex;
  612. if (-1 != pbps->imageIndex) pbps->hImage = (HBITMAP)buttonIL.hmlil;
  613. else pbps->hImage = NULL;
  614. }
  615. else
  616. {
  617. pbps->hImage = (HBITMAP)SendMessageW(hwnd, BM_GETIMAGE, IMAGE_BITMAP, 0L);
  618. pbps->imageIndex = -1;
  619. }
  620. if (pbps->cchText || pbps->hImage)
  621. {
  622. switch((BS_CENTER & pbps->windowStyle))
  623. {
  624. case BS_LEFT: pbps->textFormat |= DT_LEFT; break;
  625. case BS_RIGHT: pbps->textFormat |= DT_RIGHT; break;
  626. case BS_CENTER: pbps->textFormat |= DT_CENTER; break;
  627. case 0:
  628. switch(BS_TYPEMASK & pbps->windowStyle)
  629. {
  630. case BS_PUSHBUTTON:
  631. case BS_DEFPUSHBUTTON: pbps->textFormat |= DT_CENTER; break;
  632. default: pbps->textFormat |= DT_LEFT; break;
  633. }
  634. break;
  635. }
  636. switch((BS_VCENTER & pbps->windowStyle))
  637. {
  638. case BS_TOP: pbps->textFormat |= DT_TOP; break;
  639. case BS_BOTTOM: pbps->textFormat |= DT_BOTTOM; break;
  640. case 0:
  641. case BS_VCENTER: pbps->textFormat |= DT_VCENTER; break;
  642. }
  643. }
  644. }
  645. void SkinnedButton::OnPaint()
  646. {
  647. PAINTSTRUCT ps;
  648. BUTTONPAINTSTRUCT bps;
  649. PrePaint(hwnd, &bps, style, uiState);
  650. bps.hdc = BeginPaint(hwnd, &ps);
  651. if (NULL == bps.hdc) return;
  652. CopyRect(&bps.rcPaint, &ps.rcPaint);
  653. DrawButton(&bps);
  654. EndPaint(hwnd, &ps);
  655. }
  656. void SkinnedButton::OnPrintClient(HDC hdc, UINT options)
  657. {
  658. if ((PRF_CLIENT & options) && (0 == (PRF_CHECKVISIBLE & options) || IsWindowVisible(hwnd)))
  659. {
  660. BUTTONPAINTSTRUCT bps;
  661. PrePaint(hwnd, &bps, style, uiState);
  662. bps.hdc = hdc;
  663. CopyRect(&bps.rcPaint, &bps.rcClient);
  664. DrawButton(&bps);
  665. }
  666. }
  667. void SkinnedButton::Emulate_LeftButtonDown(UINT flags, POINTS pts, BOOL forwardMessage)
  668. {
  669. if(SWBS_SPLITBUTTON & style)
  670. {
  671. POINT pt;
  672. RECT rc;
  673. UINT os = (style & ~SWBS_SPLITSETMANUAL);
  674. POINTSTOPOINT(pt, pts);
  675. GetClientRect(hwnd, &rc);
  676. rc.left = rc.right - SPLITAREA_WIDTH;
  677. style &= ~(SWBS_SPLITPRESSED | SWBS_SPLITSETMANUAL);
  678. if (PtInRect(&rc, pt))
  679. {
  680. style |= SWBS_SPLITPRESSED;
  681. if (0 == (SWBS_TOOLBAR & style) && hwnd != GetFocus()) SetFocus(hwnd);
  682. }
  683. if (style != os) InvalidateRect(hwnd, NULL, FALSE);
  684. if (SWBS_SPLITPRESSED & style)
  685. {
  686. return;
  687. }
  688. }
  689. else if (SWBS_DROPDOWNBUTTON & style)
  690. {
  691. UINT os = (style & ~SWBS_SPLITSETMANUAL);
  692. style &= ~SWBS_SPLITSETMANUAL;
  693. style |= SWBS_SPLITPRESSED;
  694. if ( 0 == (SWBS_TOOLBAR & style) && hwnd != GetFocus()) SetFocus(hwnd);
  695. if (style != os) InvalidateRect(hwnd, NULL, FALSE);
  696. return;
  697. }
  698. if (SWBS_TOOLBAR & style)
  699. {
  700. if (lButtonDownHWND != hwnd)
  701. {
  702. lButtonDownHWND = hwnd;
  703. SendMessageW(hwnd, BM_SETSTATE, TRUE, 0L);
  704. InvalidateRect(hwnd, NULL, FALSE);
  705. UpdateWindow(hwnd);
  706. }
  707. return;
  708. }
  709. UINT s = (UINT)SendMessageW(hwnd, BM_GETSTATE, 0, 0L);
  710. if (BST_PUSHED & s) return;
  711. if (FALSE != forwardMessage)
  712. {
  713. DisableRedraw();
  714. CallPrevWndProc(WM_LBUTTONDOWN, (WPARAM)flags, *((LONG*)&pts));
  715. EnableRedraw(SWR_INVALIDATE | SWR_UPDATE);
  716. }
  717. }
  718. void SkinnedButton::Emulate_LeftButtonUp(UINT flags, POINTS pts, BOOL forwardMessage)
  719. {
  720. if ((SWBS_DROPDOWNBUTTON | SWBS_SPLITBUTTON) & style)
  721. {
  722. if (SWBS_SPLITPRESSED & style)
  723. {
  724. SendMessageW(GetParent(hwnd), WM_COMMAND, MAKELONG(GetDlgCtrlID(hwnd), MLBN_DROPDOWN), (LPARAM)hwnd);
  725. if (0 == (SWBS_SPLITSETMANUAL &style))
  726. {
  727. style &= ~SWBS_SPLITPRESSED;
  728. InvalidateRect(hwnd, NULL, FALSE);
  729. return;
  730. }
  731. }
  732. }
  733. if (FALSE != forwardMessage)
  734. {
  735. __super::CallPrevWndProc(WM_LBUTTONUP, (WPARAM)flags, *((LONG*)&pts));
  736. }
  737. if (lButtonDownHWND)
  738. {
  739. lButtonDownHWND = FALSE;
  740. HWND hParent = GetParent(hwnd);
  741. SendMessageW(hwnd, BM_SETSTATE, FALSE, 0L);
  742. DWORD ws = GetWindowLongPtrW(hwnd, GWL_STYLE);
  743. INT type = (BS_TYPEMASK & ws);
  744. switch(type)
  745. {
  746. case BS_AUTO3STATE:
  747. case BS_AUTORADIOBUTTON:
  748. case BS_AUTOCHECKBOX:
  749. {
  750. INT state = (INT)SendMessageW(hwnd, BM_GETCHECK, 0, 0L);
  751. if (BS_AUTORADIOBUTTON == type && BST_CHECKED == state) break;
  752. switch(state)
  753. {
  754. case BST_CHECKED:
  755. state = (BS_AUTO3STATE == type) ? BST_INDETERMINATE : BST_UNCHECKED;
  756. break;
  757. case BST_INDETERMINATE:
  758. case BST_UNCHECKED:
  759. state = BST_CHECKED;
  760. break;
  761. }
  762. if (BS_AUTORADIOBUTTON == type)
  763. {
  764. HWND h = hwnd;
  765. wchar_t szClass[32] = {0};
  766. DWORD lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  767. while (NULL != (h = GetWindow(h, GW_HWNDPREV)))
  768. {
  769. if (!GetClassNameW(h, szClass, sizeof(szClass)/sizeof(szClass[0])) ||
  770. CSTR_EQUAL != CompareStringW(lcid, NORM_IGNORECASE, WC_BUTTONW, -1, szClass, -1)) break;
  771. SendMessageW(h, BM_SETCHECK, BST_UNCHECKED, 0L);
  772. if (WS_GROUP & GetWindowLongPtrW(h, GWL_STYLE)) break;
  773. }
  774. SendMessageW(hwnd, BM_SETCHECK, state, 0L);
  775. h = hwnd;
  776. while (NULL != (h = GetWindow(h, GW_HWNDNEXT)))
  777. {
  778. if (!GetClassNameW(h, szClass, sizeof(szClass)/sizeof(szClass[0])) ||
  779. CSTR_EQUAL != CompareStringW(lcid, NORM_IGNORECASE, WC_BUTTONW, -1, szClass, -1)) break;
  780. SendMessageW(h, BM_SETCHECK, BST_UNCHECKED, 0L);
  781. if (WS_GROUP & GetWindowLongPtrW(h, GWL_STYLE)) break;
  782. }
  783. }
  784. else SendMessageW(hwnd, BM_SETCHECK, state, 0L);
  785. }
  786. break;
  787. }
  788. if (hParent)
  789. SendMessageW(hParent, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(hwnd), BN_CLICKED), (LPARAM)hwnd);
  790. }
  791. UpdateWindow(hwnd);
  792. }
  793. void SkinnedButton::OnLButtonDown(UINT flags, POINTS pts)
  794. {
  795. Emulate_LeftButtonDown(flags, pts, TRUE);
  796. }
  797. void SkinnedButton::OnLButtonUp(UINT flags, POINTS pts)
  798. {
  799. Emulate_LeftButtonUp(flags, pts, TRUE);
  800. }
  801. void SkinnedButton::OnMouseMove(UINT flags, POINTS pts)
  802. {
  803. BOOL bInvalidate(FALSE);
  804. if (hwnd != hoverHWND && (!lButtonDownHWND || lButtonDownHWND == hwnd) && (0 == (SWBS_TOOLBAR & style) || IsChild(GetActiveWindow(), hwnd)))
  805. {
  806. if (NULL != hoverHWND && hoverHWND != hwnd) SendMessageW(hoverHWND, WM_MOUSELEAVE, 0, 0L);
  807. hoverHWND = hwnd;
  808. if (lButtonDownHWND == hwnd) SendMessageW(hwnd, BM_SETSTATE, TRUE, 0L);
  809. bInvalidate = TRUE;
  810. hoverSplit = (SWBS_DROPDOWNBUTTON & style);
  811. TRACKMOUSEEVENT track;
  812. track.cbSize = sizeof(TRACKMOUSEEVENT);
  813. track.dwFlags = TME_LEAVE;
  814. track.hwndTrack = hwnd;
  815. track.dwHoverTime = HOVER_DEFAULT;
  816. _TrackMouseEvent(&track);
  817. }
  818. if (SWBS_SPLITBUTTON == ((SWBS_SPLITPRESSED | SWBS_SPLITBUTTON) & style))
  819. {
  820. BOOL bSplit;
  821. POINT pt;
  822. RECT rc;
  823. POINTSTOPOINT(pt, pts);
  824. GetClientRect(hwnd, &rc);
  825. rc.left = rc.right - SPLITAREA_WIDTH;
  826. bSplit = PtInRect(&rc, pt);
  827. if (hoverSplit != bSplit)
  828. {
  829. hoverSplit = bSplit;
  830. bInvalidate = TRUE;
  831. }
  832. }
  833. __super::CallPrevWndProc(WM_MOUSEMOVE, (WPARAM)flags, *((LONG*)&pts));
  834. if (bInvalidate)
  835. {
  836. InvalidateRect(hwnd, NULL, FALSE);
  837. UpdateWindow(hwnd);
  838. }
  839. }
  840. LRESULT SkinnedButton::GetIdealSize(LPCWSTR pszText)
  841. {
  842. INT cchText;
  843. SIZE szButton;
  844. szButton.cx = 0;
  845. szButton.cy = 0;
  846. cchText = (pszText) ? lstrlenW(pszText) : (INT)SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0L);
  847. {
  848. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE);
  849. if (hdc)
  850. {
  851. wchar_t szText[BUTTON_TEXT_MAX] = {0};
  852. if (NULL == pszText)
  853. {
  854. SendMessageW(hwnd, WM_GETTEXT, (WPARAM)BUTTON_TEXT_MAX, (LPARAM)szText);
  855. pszText = szText;
  856. }
  857. HFONT hFont = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0L);
  858. if (NULL == hFont) hFont = (HFONT)MlStockObjects_Get(DEFAULT_FONT);
  859. HFONT hfo = (NULL != hFont) ? (HFONT)SelectObject(hdc, hFont) : NULL;
  860. if (0 != cchText)
  861. {
  862. RECT rt;
  863. SetRect(&rt, 0, 0, 0, 0);
  864. if (FALSE == DrawTextW(hdc, pszText, cchText, &rt, DT_CALCRECT | DT_SINGLELINE))
  865. {
  866. szButton.cx = 0;
  867. szButton.cy = 0;
  868. }
  869. else
  870. {
  871. szButton.cx = rt.right - rt.left;
  872. szButton.cy = rt.bottom - rt.top;
  873. }
  874. }
  875. else
  876. {
  877. TEXTMETRIC metrics;
  878. szButton.cx = 0;
  879. if (FALSE == GetTextMetrics(hdc, &metrics))
  880. szButton.cy = 0;
  881. else
  882. szButton.cy = metrics.tmHeight;
  883. }
  884. if (0 != szButton.cy)
  885. szButton.cy += (MARGIN_TOP + MARGIN_BOTTOM);
  886. if (0 != szButton.cx)
  887. szButton.cx += (MARGIN_LEFT + MARGIN_RIGHT) + 2;
  888. if (NULL != hfo)
  889. SelectObject(hdc, hfo);
  890. ReleaseDC(hwnd, hdc);
  891. }
  892. }
  893. if (SWBS_DROPDOWNBUTTON & style) szButton.cx += (SPLITAREA_WIDTH + SPLITAREA_SPACE);
  894. if (SWBS_SPLITBUTTON & style) szButton.cx += (SPLITAREA_WIDTH + SPLITAREA_SPACE_ALT);
  895. MLBUTTONIMAGELIST buttonIL;
  896. if(MLSkinnedButton_GetImageList(hwnd, &buttonIL) && buttonIL.hmlil)
  897. {
  898. INT imageCX, imageCY;
  899. if (MLImageListI_GetImageSize(buttonIL.hmlil, &imageCX, &imageCY))
  900. {
  901. imageCY += (MARGIN_TOP + MARGIN_BOTTOM);
  902. if (szButton.cy < imageCY) szButton.cy = imageCY;
  903. szButton.cx += imageCX;
  904. if (szButton.cx != imageCX) szButton.cx += IMAGE_SPACE;
  905. else szButton.cx += (MARGIN_LEFT + MARGIN_RIGHT);
  906. }
  907. }
  908. return MAKELPARAM(szButton.cx, szButton.cy);
  909. }
  910. BOOL SkinnedButton::OnMediaLibraryIPC(INT msg, INT_PTR param, LRESULT *pResult)
  911. {
  912. switch(msg)
  913. {
  914. case ML_IPC_SKINNEDBUTTON_SETIMAGELIST:
  915. ZeroMemory(&imagelist, sizeof(MLBUTTONIMAGELIST));
  916. if (param) CopyMemory(&imagelist, (void*)param, sizeof(MLBUTTONIMAGELIST));
  917. *pResult = TRUE;
  918. InvalidateRect(hwnd, NULL, FALSE);
  919. return TRUE;
  920. case ML_IPC_SKINNEDBUTTON_GETIMAGELIST:
  921. if (!param) *pResult = FALSE;
  922. else
  923. {
  924. CopyMemory((void*)param, &imagelist, sizeof(MLBUTTONIMAGELIST));
  925. *pResult = TRUE;
  926. }
  927. return TRUE;
  928. case ML_IPC_SKINNEDBUTTON_SETDROPDOWNSTATE:
  929. {
  930. UINT os = style;
  931. style &= ~SWBS_SPLITPRESSED;
  932. if (param) style |= SWBS_SPLITPRESSED;
  933. style |= SWBS_SPLITSETMANUAL;
  934. if (style != os) InvalidateRect(hwnd, NULL, FALSE);
  935. }
  936. return TRUE;
  937. case ML_IPC_SKINNEDBUTTON_GETIDEALSIZE:
  938. *pResult = GetIdealSize((LPCWSTR)param);
  939. return TRUE;
  940. }
  941. return __super::OnMediaLibraryIPC(msg, param, pResult);
  942. }
  943. LRESULT SkinnedButton::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  944. {
  945. if (SWS_USESKINCOLORS & style)
  946. {
  947. switch(uMsg)
  948. {
  949. case WM_PAINT: OnPaint(); return 0;
  950. case WM_ERASEBKGND: return 0;
  951. case WM_PRINTCLIENT: OnPrintClient((HDC)wParam, (UINT)lParam); return 0;
  952. case WM_SETFOCUS:
  953. case WM_KILLFOCUS:
  954. case 0x0128/*WM_UPDATEUISTATE*/:
  955. case WM_SETTEXT:
  956. case WM_ENABLE:
  957. {
  958. DisableRedraw();
  959. LRESULT r = __super::WindowProc(uMsg, wParam, lParam);
  960. EnableRedraw(SWR_INVALIDATE | SWR_UPDATE);
  961. return r;
  962. }
  963. case BM_SETSTATE:
  964. if (GetUpdateRect(hwnd, NULL, FALSE)) UpdateWindow(hwnd);
  965. {
  966. LRESULT os = SendMessageW(hwnd, BM_GETSTATE, 0, 0L);
  967. DisableRedraw();
  968. __super::WindowProc(uMsg, wParam, lParam);
  969. EnableRedraw(SWR_NONE);
  970. if (os != SendMessageW(hwnd, BM_GETSTATE, 0, 0L))
  971. {
  972. InvalidateRect(hwnd, NULL, FALSE);
  973. UpdateWindow(hwnd);
  974. }
  975. }
  976. break;
  977. case BM_SETCHECK:
  978. if (GetUpdateRect(hwnd, NULL, FALSE)) UpdateWindow(hwnd);
  979. {
  980. LRESULT os = SendMessageW(hwnd, BM_GETCHECK, 0, 0L);
  981. DisableRedraw();
  982. __super::WindowProc(uMsg, wParam, lParam);
  983. EnableRedraw(SWR_NONE);
  984. if (os != SendMessageW(hwnd, BM_GETCHECK, 0, 0L))
  985. {
  986. InvalidateRect(hwnd, NULL, FALSE);
  987. UpdateWindow(hwnd);
  988. }
  989. }
  990. break;
  991. case 0x0127/*WM_CHANGEUISTATE*/:
  992. {
  993. if (GetUpdateRect(hwnd, NULL, FALSE)) UpdateWindow(hwnd);
  994. DisableRedraw();
  995. LRESULT r = __super::WindowProc(uMsg, wParam, lParam);
  996. EnableRedraw(SWR_NONE);
  997. return r;
  998. }
  999. case WM_GETDLGCODE:
  1000. {
  1001. LRESULT r = __super::WindowProc(uMsg, wParam, lParam);
  1002. if ((SWBS_SPLITBUTTON | SWBS_DROPDOWNBUTTON) & style)
  1003. r |= DLGC_WANTARROWS;
  1004. return r;
  1005. }
  1006. case WM_KEYDOWN:
  1007. switch(wParam)
  1008. {
  1009. case VK_LEFT:
  1010. case VK_RIGHT:
  1011. if ((SWBS_SPLITBUTTON | SWBS_DROPDOWNBUTTON) & style)
  1012. {
  1013. HWND hwndParent = GetParent(hwnd);
  1014. if (hwndParent) SendMessageW(hwndParent, WM_NEXTDLGCTL, (VK_LEFT == wParam), 0L);
  1015. SendMessageW(hwnd, 0x127/*WM_CHANGEUISTATE*/, MAKELONG(2/*UIS_CLEAR*/, 1/*UISF_HIDEFOCUS*/), 0L);
  1016. return 0;
  1017. }
  1018. break;
  1019. case VK_DOWN:
  1020. case VK_UP:
  1021. case VK_SPACE:
  1022. if (((SWBS_SPLITBUTTON | SWBS_DROPDOWNBUTTON) & style) &&
  1023. 1 == LOWORD(lParam) && 0 == (0x40000000 & lParam))
  1024. {
  1025. RECT rc;
  1026. GetClientRect(hwnd, &rc);
  1027. if (VK_SPACE != wParam) rc.left = rc.right - SPLITAREA_WIDTH;
  1028. OffsetRect(&rc, 1, 1);
  1029. LONG pts = POINTTOPOINTS(*(POINT*)&rc);
  1030. Emulate_LeftButtonDown(0, MAKEPOINTS(pts), FALSE);
  1031. SendMessageW(hwnd, 0x127/*WM_CHANGEUISTATE*/, MAKELONG(2/*UIS_CLEAR*/, 1/*UISF_HIDEFOCUS*/), 0L);
  1032. return 0;
  1033. }
  1034. break;
  1035. }
  1036. break;
  1037. case WM_KEYUP:
  1038. switch(wParam)
  1039. {
  1040. case VK_DOWN:
  1041. case VK_UP:
  1042. case VK_SPACE:
  1043. if (((SWBS_SPLITBUTTON | SWBS_DROPDOWNBUTTON) & style) &&
  1044. 1 == LOWORD(lParam))
  1045. {
  1046. RECT rc;
  1047. GetClientRect(hwnd, &rc);
  1048. if (VK_SPACE != wParam) rc.left = rc.right - SPLITAREA_WIDTH;
  1049. OffsetRect(&rc, 1, 1);
  1050. LONG pts = POINTTOPOINTS(*(POINT*)&rc);
  1051. Emulate_LeftButtonUp(0, MAKEPOINTS(pts), FALSE);
  1052. return 0;
  1053. }
  1054. break;
  1055. }
  1056. break;
  1057. case WM_LBUTTONDBLCLK:
  1058. case WM_LBUTTONDOWN: OnLButtonDown((UINT)wParam, MAKEPOINTS(lParam)); return 0;
  1059. case WM_MOUSEMOVE: OnMouseMove((UINT)wParam, MAKEPOINTS(lParam)); return 0;
  1060. case WM_LBUTTONUP: OnLButtonUp((UINT)wParam, MAKEPOINTS(lParam)); return 0;
  1061. case WM_MOUSELEAVE:
  1062. if (hwnd == hoverHWND ||
  1063. (SWBS_SPLITPRESSED | SWBS_SPLITBUTTON) == ((SWBS_SPLITPRESSED | SWBS_SPLITBUTTON | SWBS_SPLITSETMANUAL) & style))
  1064. {
  1065. hoverHWND = NULL;
  1066. hoverSplit = FALSE;
  1067. if (0 == (SWBS_SPLITSETMANUAL &style)) style &= ~SWBS_SPLITPRESSED;
  1068. InvalidateRect(hwnd, NULL, FALSE);
  1069. UpdateWindow(hwnd);
  1070. }
  1071. if (lButtonDownHWND == hwnd) SendMessageW(hwnd, BM_SETSTATE, FALSE, 0L);
  1072. break;
  1073. }
  1074. }
  1075. return __super::WindowProc(uMsg, wParam, lParam);
  1076. }
  1077. static BOOL CALLBACK ImageFilter(LPBYTE pData, LONG cx, LONG cy, INT bpp, COLORREF rgbBk, COLORREF rgbFg, INT_PTR imageTag, LPARAM lParam)
  1078. {
  1079. LONG pitch, x, dibPitch;
  1080. LPBYTE dib, cursor, line;
  1081. HDC hdc, hdcSrc;
  1082. HBITMAP hbmp, hbmpOld;
  1083. IMAGEFILTERPARAM *param;
  1084. param = &imagefilterParam;
  1085. if (32 != bpp || !param)
  1086. return FALSE;
  1087. pitch = cx*4;
  1088. hdcSrc = param->pbps->hdc;
  1089. rgbFg = param->pbps->rgbText;
  1090. rgbBk = param->pbps->rgbTextBk;
  1091. BITMAPINFOHEADER bi;
  1092. ZeroMemory(&bi, sizeof(bi));
  1093. bi.biSize = sizeof(bi);
  1094. bi.biWidth = param->pbps->rcClient.right - param->pbps->rcClient.left;
  1095. bi.biHeight = -(param->pbps->rcClient.bottom - param->pbps->rcClient.top);
  1096. bi.biPlanes = 1;
  1097. bi.biBitCount = 32;
  1098. hdc = CreateCompatibleDC(NULL);
  1099. if (NULL == hdc)
  1100. return FALSE;
  1101. IntersectClipRect(hdc, param->prcImage->left, param->prcImage->top,
  1102. param->prcImage->right, param->prcImage->bottom);
  1103. hbmp = CreateDIBSection(hdc, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (VOID**)&dib, NULL, NULL);
  1104. if (NULL == hbmp)
  1105. {
  1106. DeleteDC(hdc);
  1107. return FALSE;
  1108. }
  1109. hbmpOld = (HBITMAP)SelectObject(hdc, hbmp);
  1110. param->pbps->hdc = hdc;
  1111. RECT rcPaint;
  1112. CopyRect(&rcPaint, &param->pbps->rcPaint);
  1113. CopyRect(&param->pbps->rcPaint, param->prcImage);
  1114. PushButton_DrawBackground(param->pbps);
  1115. param->pbps->hdc = hdcSrc;
  1116. CopyRect(&param->pbps->rcPaint, &rcPaint);
  1117. SelectObject(hdc, hbmpOld);
  1118. DeleteDC(hdc);
  1119. dibPitch = 4*(bi.biWidth);
  1120. dib += 4*(param->prcImage->left) + param->prcImage->top*dibPitch;
  1121. cy = param->prcImage->bottom - param->prcImage->top;
  1122. for (line = pData; cy-- != 0; line += pitch, dib += dibPitch )
  1123. {
  1124. for (x = cx, cursor = line; x-- != 0; cursor += 4)
  1125. {
  1126. if (IMAGEFILTER_BLENDPLUSCOLOR == lParam)
  1127. {
  1128. BYTE luma;
  1129. luma = 255 - (BYTE)((cursor[2]*30 + cursor[1]*59 + cursor[0]*11)/100);
  1130. cursor[0] = GetBValue(rgbFg) - ((GetBValue(rgbFg) - GetBValue(rgbBk))*luma>>8);
  1131. cursor[1] = GetGValue(rgbFg) - ((GetGValue(rgbFg) - GetGValue(rgbBk))*luma>>8);
  1132. cursor[2] = GetRValue(rgbFg) - ((GetRValue(rgbFg) - GetRValue(rgbBk))*luma>>8);
  1133. }
  1134. if (0x00 == cursor[3])
  1135. {
  1136. cursor[0] = dib[0];
  1137. cursor[1] = dib[1];
  1138. cursor[2] = dib[2];
  1139. cursor[3] = 0xFF;
  1140. }
  1141. else if (cursor[3] != 0xFF)
  1142. {
  1143. cursor[0] = (cursor[0]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*dib[0] + 127)/255;
  1144. cursor[1] = (cursor[1]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*dib[1] + 127)/255;
  1145. cursor[2] = (cursor[2]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*dib[2] + 127)/255;
  1146. cursor[3] = 0xFF;
  1147. }
  1148. }
  1149. }
  1150. DeleteObject(hbmp); ///Last call
  1151. return TRUE;
  1152. }
  1153. BOOL SkinnedButton::RegisterImageFilter(HANDLE filterMananger)
  1154. {
  1155. BOOL result;
  1156. MLIMAGEFILTERINFO_I mlif;
  1157. ZeroMemory(&mlif, sizeof(MLIMAGEFILTERINFO_I));
  1158. mlif.mask = MLIFF_TITLE_I | MLIFF_FLAGS_I | MLIFF_PROC_I | MLIFF_PARAM_I;
  1159. mlif.uid = MLIF_BUTTONBLEND_UID;
  1160. mlif.fnProc = ImageFilter;
  1161. mlif.pszTitle = L"Button image filter (blend)";
  1162. mlif.lParam = IMAGEFILTER_BLEND;
  1163. mlif.fFlags = 0;
  1164. result = MLImageFilterI_Register((HMLIMGFLTRMNGR)filterMananger, &mlif);
  1165. mlif.mask = MLIFF_TITLE_I | MLIFF_FLAGS_I | MLIFF_PROC_I | MLIFF_PARAM_I;
  1166. mlif.uid = MLIF_BUTTONBLENDPLUSCOLOR_UID;
  1167. mlif.fnProc = ImageFilter;
  1168. mlif.pszTitle = L"Button image filter (blend plus color)";
  1169. mlif.lParam = IMAGEFILTER_BLENDPLUSCOLOR;
  1170. mlif.fFlags = 0;
  1171. if (!MLImageFilterI_Register((HMLIMGFLTRMNGR)filterMananger, &mlif)) result = FALSE;
  1172. return result;
  1173. }