skinnedlistbox.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "./skinnedlistbox.h"
  2. #include "../winamp/wa_dlg.h"
  3. #include "./skinning.h"
  4. #include "../nu/trace.h"
  5. #include <strsafe.h>
  6. SkinnedListbox::SkinnedListbox(void) : SkinnedScrollWnd(FALSE)
  7. {
  8. }
  9. SkinnedListbox::~SkinnedListbox(void)
  10. {
  11. }
  12. BOOL SkinnedListbox::Attach(HWND hwndListbox)
  13. {
  14. DWORD ws;
  15. if(!SkinnedScrollWnd::Attach(hwndListbox)) return FALSE;
  16. ws = GetWindowLongPtrW(hwnd, GWL_STYLE);
  17. SetType(SKINNEDWND_TYPE_LISTBOX);
  18. SetMode((LBS_COMBOBOX & ws) ? SCROLLMODE_COMBOLBOX : SCROLLMODE_STANDARD);
  19. if (0 == (LBS_COMBOBOX & ws))
  20. {
  21. HWND hwndParent = GetParent(hwndListbox);
  22. if (hwndParent) SkinWindow(hwndParent, SWS_NORMAL);
  23. }
  24. if (LBS_DISABLENOSCROLL & ws) DisableNoScroll(TRUE);
  25. SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
  26. return TRUE;
  27. }
  28. HPEN SkinnedListbox::GetBorderPen(void)
  29. {
  30. if (LBS_COMBOBOX & GetWindowLongPtrW(hwnd, GWL_STYLE))
  31. {
  32. return (HPEN)MlStockObjects_Get(MENUBORDER_PEN);
  33. }
  34. else return __super::GetBorderPen();
  35. }
  36. void SkinnedListbox::OnDrawItem(DRAWITEMSTRUCT *pdis)
  37. {
  38. static wchar_t szText[512];
  39. if (pdis->itemID == -1) return;
  40. INT cchLen = (INT)SendMessageW(pdis->hwndItem, LB_GETTEXT, pdis->itemID, (LPARAM)szText);
  41. DrawItem(pdis, szText, cchLen);
  42. }
  43. void SkinnedListbox::DrawItem(DRAWITEMSTRUCT *pdis, LPCWSTR pszText, INT cchText)
  44. {
  45. RECT ri;
  46. COLORREF rgbTextOld, rgbBkOld;
  47. if (pdis->itemID == -1) return;
  48. CopyRect(&ri, &pdis->rcItem);
  49. if (ODA_FOCUS == pdis->itemAction)
  50. {
  51. if (0 == (0x0200/*ODS_NOFOCUSRECT*/ & pdis->itemState))
  52. {
  53. rgbTextOld = SetTextColor(pdis->hDC, 0x000000);
  54. rgbBkOld = SetBkColor(pdis->hDC, 0xFFFFFF);
  55. DrawFocusRect(pdis->hDC, &ri);
  56. SetTextColor(pdis->hDC, rgbTextOld);
  57. SetBkColor(pdis->hDC, rgbBkOld);
  58. }
  59. return;
  60. }
  61. if (ODS_SELECTED & pdis->itemState)
  62. {
  63. BOOL bActive = (pdis->hwndItem == GetFocus());
  64. if (!bActive && (LBS_COMBOBOX & GetWindowLongPtrW(pdis->hwndItem, GWL_STYLE))) bActive = TRUE;
  65. rgbTextOld = SetTextColor(pdis->hDC, WADlg_getColor((bActive) ? WADLG_SELBAR_FGCOLOR : WADLG_INACT_SELBAR_FGCOLOR));
  66. rgbBkOld = SetBkColor(pdis->hDC, WADlg_getColor((bActive) ? WADLG_SELBAR_BGCOLOR : WADLG_INACT_SELBAR_BGCOLOR));
  67. }
  68. else
  69. {
  70. rgbTextOld = GetTextColor(pdis->hDC);
  71. rgbBkOld = GetBkColor(pdis->hDC);
  72. }
  73. if (cchText> 0)
  74. {
  75. //InflateRect(&ri, -4, -1);
  76. //int mode = SetBkMode(pdis->hDC, TRANSPARENT);
  77. //DrawTextW(pdis->hDC, pszText, cchText, &ri, DT_NOPREFIX | DT_VCENTER | DT_NOCLIP);
  78. //if (mode != TRANSPARENT) SetBkMode(pdis->hDC, mode);
  79. ExtTextOutW(pdis->hDC, ri.left + 4, ri.top + 1, ETO_OPAQUE, &ri, pszText, cchText, NULL);
  80. }
  81. else if ((ODA_SELECT | ODA_DRAWENTIRE) & pdis->itemAction || (ODS_SELECTED & pdis->itemState))
  82. {
  83. ExtTextOutW(pdis->hDC, 0, 0, ETO_OPAQUE, &ri, L"", 0, NULL);
  84. }
  85. if (ODS_SELECTED & pdis->itemState)
  86. {
  87. SetTextColor(pdis->hDC, rgbTextOld);
  88. SetBkColor(pdis->hDC, rgbBkOld);
  89. }
  90. }
  91. void SkinnedListbox::MeasureItem(HWND hwnd, LPCWSTR pszText, INT cchText, UINT *pWidth, UINT *pHeight)
  92. {
  93. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  94. HFONT hf, hfo;
  95. hf = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0L);
  96. if (NULL == hf) hf = (HFONT) MlStockObjects_Get(DEFAULT_FONT);
  97. hfo = (HFONT)SelectObject(hdc, hf);
  98. SIZE sz;
  99. if (!GetTextExtentPoint32W(hdc, pszText, cchText, &sz))
  100. ZeroMemory(&sz, sizeof(SIZE));
  101. if (pWidth) *pWidth = sz.cx;
  102. if (pHeight) *pHeight= sz.cy;
  103. SelectObject(hdc, hfo);
  104. ReleaseDC(hwnd, hdc);
  105. }
  106. void SkinnedListbox::OnSkinUpdated(BOOL bNotifyChildren, BOOL bRedraw)
  107. {
  108. __super::OnSkinUpdated(bNotifyChildren, bRedraw);
  109. if (SWS_USESKINFONT & style)
  110. {
  111. if (0 == (LBS_OWNERDRAWVARIABLE & GetWindowLongPtrW(hwnd, GWL_STYLE)))
  112. {
  113. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  114. HFONT hf, hfo;
  115. TEXTMETRIC tm;
  116. hf = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0L);
  117. if (NULL == hf) hf = (HFONT)MlStockObjects_Get(DEFAULT_FONT);
  118. hfo = (HFONT)SelectObject(hdc, hf);
  119. GetTextMetrics(hdc, &tm);
  120. SendMessageW(hwnd, LB_SETITEMHEIGHT, 0, tm.tmHeight + 2);
  121. SelectObject(hdc, hfo);
  122. ReleaseDC(hwnd, hdc);
  123. }
  124. }
  125. }
  126. void SkinnedListbox::OnPrint(HDC hdc, UINT options)
  127. {
  128. if ((PRF_CHECKVISIBLE & options) && !IsWindowVisible(hwnd)) return;
  129. if (LBS_COMBOBOX & GetWindowLongPtrW(hwnd, GWL_STYLE))
  130. {
  131. __super::OnPrint(hdc, options & ~PRF_CLIENT);
  132. if (((PRF_CLIENT | PRF_ERASEBKGND) & options))
  133. {
  134. OffsetViewportOrgEx(hdc, 1, 1, NULL);
  135. SendMessageW(hwnd, WM_PRINTCLIENT, (WPARAM)hdc, (LPARAM)((PRF_CLIENT | PRF_ERASEBKGND) & options));
  136. OffsetViewportOrgEx(hdc, -1, -1, NULL);
  137. }
  138. return;
  139. }
  140. __super::OnPrint(hdc, options);
  141. }
  142. LRESULT SkinnedListbox::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  143. {
  144. if (SWS_USESKINCOLORS & style)
  145. {
  146. switch(uMsg)
  147. {
  148. case WM_ERASEBKGND:
  149. if (LBS_OWNERDRAWFIXED & GetWindowLongPtrW(hwnd, GWL_STYLE))
  150. {
  151. RECT rc;
  152. GetClientRect(hwnd, &rc);
  153. HBRUSH hb = (HBRUSH)MlStockObjects_Get(ITEMBCK_BRUSH);
  154. INT count = GetListBoxInfo(hwnd);
  155. if (count > 0)
  156. {
  157. RECT ri;
  158. if (LB_ERR != SendMessageW(hwnd, LB_GETITEMRECT, count -1, (LPARAM)&ri))
  159. {
  160. __super::WindowProc(uMsg, 0, lParam);
  161. if (ri.bottom < rc.bottom)
  162. {
  163. rc.top = ri.bottom;
  164. FillRect((HDC)wParam, &rc, hb);
  165. }
  166. if (ri.right < rc.right)
  167. {
  168. rc.top = 0;
  169. rc.left = ri.right;
  170. FillRect((HDC)wParam, &rc, hb);
  171. }
  172. return 1;
  173. }
  174. }
  175. else
  176. {
  177. __super::WindowProc(uMsg, 0, lParam);
  178. FillRect((HDC)wParam, &rc, hb);
  179. return 1;
  180. }
  181. }
  182. break;
  183. case WM_SETFOCUS:
  184. case WM_KILLFOCUS:
  185. InvalidateRect(hwnd, NULL, TRUE);
  186. UpdateWindow(hwnd);
  187. break;
  188. case REFLECTED_DRAWITEM:
  189. if ((LBS_OWNERDRAWFIXED | LBS_HASSTRINGS) ==
  190. ((LBS_OWNERDRAWFIXED | LBS_NODATA | LBS_HASSTRINGS) & GetWindowLongPtrW(hwnd, GWL_STYLE)))
  191. {
  192. OnDrawItem((DRAWITEMSTRUCT*)((REFLECTPARAM*)lParam)->lParam);
  193. ((REFLECTPARAM*)lParam)->result = TRUE;
  194. return TRUE;
  195. }
  196. return FALSE;
  197. case REFLECTED_CTLCOLORLISTBOX:
  198. {
  199. COLORREF rgbText, rgbTextBk;
  200. rgbText = WADlg_getColor(WADLG_ITEMFG);
  201. rgbTextBk = WADlg_getColor(WADLG_ITEMBG);
  202. if(!IsWindowEnabled(hwnd))
  203. {
  204. rgbText = RGB((GetRValue(rgbText)+GetRValue(rgbTextBk))/2,
  205. (GetGValue(rgbText)+GetGValue(rgbTextBk))/2,
  206. (GetBValue(rgbText)+GetBValue(rgbTextBk))/2);
  207. }
  208. SetBkColor((HDC)wParam, rgbTextBk);
  209. SetTextColor((HDC)wParam, rgbText);
  210. }
  211. ((REFLECTPARAM*)lParam)->result = (LRESULT)MlStockObjects_Get(ITEMBCK_BRUSH);
  212. return TRUE;
  213. }
  214. }
  215. return __super::WindowProc(uMsg, wParam, lParam);
  216. }