setupListboxLabel.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "./setupListboxLabel.h"
  2. #include "../api__ml_online.h"
  3. #include "../common.h"
  4. #include <shlwapi.h>
  5. #include <strsafe.h>
  6. #define LABEL_MARGINCX 0
  7. #define LABEL_MARGINCY 1
  8. #define TEXT_OFFSET_LEFT 2
  9. #define TEXT_OFFSET_BOTTOM 2
  10. #define TEXT_ALIGN (TA_CENTER | TA_BOTTOM)
  11. SetupListboxLabel::SetupListboxLabel(LPCWSTR pszName)
  12. : ref(1), name(NULL)
  13. {
  14. SetName(pszName);
  15. }
  16. SetupListboxLabel::~SetupListboxLabel()
  17. {
  18. SetName(NULL);
  19. }
  20. SetupListboxLabel *SetupListboxLabel::CreateInstance(LPCWSTR pszName)
  21. {
  22. return new SetupListboxLabel(pszName);
  23. }
  24. ULONG SetupListboxLabel::AddRef()
  25. {
  26. return InterlockedIncrement((LONG*)&ref);
  27. }
  28. ULONG SetupListboxLabel::Release()
  29. {
  30. if (0 == ref)
  31. return ref;
  32. LONG r = InterlockedDecrement((LONG*)&ref);
  33. if (0 == r)
  34. delete(this);
  35. return r;
  36. }
  37. HRESULT SetupListboxLabel::GetName(LPWSTR pszBuffer, INT cchBufferMax)
  38. {
  39. if (NULL == pszBuffer)
  40. return E_POINTER;
  41. if (NULL != name && IS_INTRESOURCE(name))
  42. {
  43. WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)name, pszBuffer, cchBufferMax);
  44. return (L'\0' != *pszBuffer) ? S_OK : E_FAIL;
  45. }
  46. return StringCchCopyExW(pszBuffer, cchBufferMax, name, NULL, NULL, STRSAFE_IGNORE_NULLS);
  47. }
  48. HRESULT SetupListboxLabel::SetName(LPCWSTR pszName)
  49. {
  50. if (NULL != name && !IS_INTRESOURCE(name))
  51. Plugin_FreeString(name);
  52. if (IS_INTRESOURCE(pszName))
  53. {
  54. name = (LPWSTR)pszName;
  55. }
  56. else
  57. {
  58. name = Plugin_CopyString(pszName);
  59. }
  60. return S_OK;
  61. }
  62. BOOL SetupListboxLabel::IsNameNull()
  63. {
  64. return (NULL == name);
  65. }
  66. void SetupListboxLabel::GetColors(HDC hdc, UINT state, COLORREF *rgbBkOut, COLORREF *rgbTextOut)
  67. {
  68. COLORREF rgbBk, rgbText;
  69. rgbBk = GetSysColor(COLOR_WINDOW);
  70. rgbText = GetSysColor( (0 == (ODS_DISABLED & state)) ? COLOR_GRAYTEXT : COLOR_GRAYTEXT);
  71. if (NULL != rgbBkOut) *rgbBkOut = rgbBk;
  72. if (NULL != rgbTextOut) *rgbTextOut = rgbText;
  73. }
  74. HBRUSH SetupListboxLabel::GetBrush(HDC hdc, UINT state)
  75. {
  76. return GetSysColorBrush(COLOR_WINDOW);
  77. }
  78. BOOL SetupListboxLabel::MeasureItem(SetupListbox *instance, UINT *cx, UINT *cy)
  79. {
  80. HDC hdc = GetDCEx(instance->GetHwnd(), NULL, DCX_CACHE | DCX_NORESETATTRS);
  81. if (NULL == hdc) return FALSE;
  82. HFONT originalFont = (HFONT)SelectObject(hdc, instance->GetFont());
  83. if (NULL != cy)
  84. {
  85. *cy = 0;
  86. TEXTMETRIC tm;
  87. if (GetTextMetrics(hdc, &tm))
  88. *cy = tm.tmHeight + tm.tmExternalLeading + LABEL_MARGINCY*2;
  89. }
  90. if (NULL != cx)
  91. {
  92. *cx = 0;
  93. WCHAR szBuffer[128] = {0};
  94. if (SUCCEEDED(GetName(szBuffer, ARRAYSIZE(szBuffer))))
  95. {
  96. INT cchBuffer = lstrlenW(szBuffer);
  97. SIZE textSize;
  98. if (0 != cchBuffer && GetTextExtentPoint32(hdc, szBuffer, cchBuffer, &textSize))
  99. {
  100. *cx = textSize.cx + LABEL_MARGINCX*2;
  101. }
  102. }
  103. }
  104. SelectObject(hdc, originalFont);
  105. ReleaseDC(instance->GetHwnd(), hdc);
  106. return TRUE;
  107. }
  108. BOOL SetupListboxLabel::DrawItem(SetupListbox *instance, HDC hdc, const RECT *prc, UINT state)
  109. {
  110. LONG paintLeft = prc->left + LABEL_MARGINCX;
  111. COLORREF rgbBk, rgbText;
  112. GetColors(hdc, state, &rgbBk, &rgbText);
  113. COLORREF origBk = SetBkColor(hdc, rgbBk);
  114. COLORREF origText = SetTextColor(hdc, rgbText);
  115. UINT textAlign = SetTextAlign(hdc, TEXT_ALIGN);
  116. HRGN backRgn, rgn;
  117. backRgn = CreateRectRgnIndirect(prc);
  118. rgn = CreateRectRgn(0,0,0,0);
  119. RECT partRect;
  120. WCHAR szBuffer[128] = {0};
  121. INT cchBuffer = 0;
  122. if (SUCCEEDED(GetName(szBuffer, ARRAYSIZE(szBuffer))))
  123. cchBuffer = lstrlenW(szBuffer);
  124. SetRect(&partRect, paintLeft, prc->top, prc->right - LABEL_MARGINCX, prc->bottom);
  125. if (ExtTextOut(hdc, partRect.left + (partRect.right - partRect.left)/2, partRect.bottom - TEXT_OFFSET_BOTTOM,
  126. ETO_OPAQUE | ETO_CLIPPED, &partRect, szBuffer, cchBuffer, NULL))
  127. {
  128. if (SetRectRgn(rgn, partRect.left, partRect.top, partRect.right, partRect.bottom))
  129. CombineRgn(backRgn, backRgn, rgn, RGN_DIFF);
  130. }
  131. if (NULL != backRgn)
  132. {
  133. FillRgn(hdc, backRgn, GetBrush(hdc, state));
  134. DeleteObject(backRgn);
  135. }
  136. if (NULL != rgn)
  137. DeleteObject(rgn);
  138. if (ODS_FOCUS == ((ODS_FOCUS | 0x0200/*ODS_NOFOCUSRECT*/) & state))
  139. DrawFocusRect(hdc, prc);
  140. if (TEXT_ALIGN != textAlign) SetTextAlign(hdc, textAlign);
  141. if (origBk != rgbBk) SetBkColor(hdc, origBk);
  142. if (origText != rgbText) SetTextColor(hdc, origText);
  143. return TRUE;
  144. }
  145. INT_PTR SetupListboxLabel::KeyToItem(SetupListbox *instance, const RECT *prcItem, INT vKey)
  146. {
  147. return -1;
  148. }
  149. BOOL SetupListboxLabel::MouseMove(SetupListbox *instance, const RECT *prcItem, UINT mouseFlags, POINT pt)
  150. {
  151. return FALSE;
  152. }
  153. BOOL SetupListboxLabel::MouseLeave(SetupListbox *instance, const RECT *prcItem)
  154. {
  155. return FALSE;
  156. }
  157. BOOL SetupListboxLabel::LButtonDown(SetupListbox *instance, const RECT *prcItem, UINT mouseFlags, POINT pt)
  158. {
  159. return FALSE;
  160. }
  161. BOOL SetupListboxLabel::LButtonUp(SetupListbox *instance, const RECT *prcItem, UINT mouseFlags, POINT pt)
  162. {
  163. return FALSE;
  164. }
  165. BOOL SetupListboxLabel::LButtonDblClk(SetupListbox *instance, const RECT *prcItem, UINT mouseFlags, POINT pt)
  166. {
  167. return FALSE;
  168. }
  169. BOOL SetupListboxLabel::RButtonDown(SetupListbox *instance, const RECT *prcItem, UINT mouseFlags, POINT pt)
  170. {
  171. return FALSE;
  172. }
  173. BOOL SetupListboxLabel::RButtonUp(SetupListbox *instance, const RECT *prcItem, UINT mouseFlags, POINT pt)
  174. {
  175. return FALSE;
  176. }
  177. void SetupListboxLabel::CaptureChanged(SetupListbox *instance, const RECT *prcItem, SetupListboxItem *captured)
  178. {
  179. }
  180. BOOL SetupListboxLabel::GetUniqueName(LPWSTR pszBuffer, UINT cchBufferMax)
  181. {
  182. WCHAR szName[128] = {0};
  183. if (FAILED(GetName(szName, ARRAYSIZE(szName))))
  184. return FALSE;
  185. if (NULL == pszBuffer ||
  186. FAILED(StringCchPrintf(pszBuffer, cchBufferMax, L"lbl_empty_%s", szName)))
  187. {
  188. return FALSE;
  189. }
  190. return TRUE;
  191. }